kelvinkurniawan/lightorm
Composer 安装命令:
composer require kelvinkurniawan/lightorm
包简介
A lightweight, modern ORM for PHP with Active Record pattern, query builder, soft deletes, and advanced features
README 文档
README
A lightweight, modern ORM for PHP with Active Record pattern, query builder, soft deletes, and advanced features.
Features
- 🚀 Active Record Pattern - Intuitive object-relational mapping
- 🔍 Advanced Query Builder - Fluent interface for complex queries
- 🗑️ Soft Deletes - Preserve data with soft delete functionality
- ⏰ Automatic Timestamps - Auto-managed created_at and updated_at
- ✅ Model Validation - Built-in validation with custom rules
- 🎯 Event System - Model lifecycle hooks
- 🔄 Attribute Casting - Automatic type conversion
- 🛡️ Mass Assignment Protection - Secure fillable/guarded attributes
- 📦 Query Scopes - Reusable query constraints
- 💾 Cache Integration - Query result caching
- 🔄 Database Transactions - Transaction support
Installation
Install via Composer:
composer require kelvinkurniawan/lightorm
Quick Start
1. Configuration
First, configure your database connection:
<?php require_once 'vendor/autoload.php'; use KelvinKurniawan\LightORM\Core\Database; // Set database configuration Database::setConfig([ 'host' => 'localhost', 'dbname' => 'your_database', 'username' => 'your_username', 'password' => 'your_password', ]);
2. Create a Model
<?php use KelvinKurniawan\LightORM\Core\Model; class User extends Model { protected static string $table = 'users'; protected array $fillable = [ 'name', 'email', 'password' ]; protected array $hidden = [ 'password' ]; protected array $casts = [ 'email_verified_at' => 'datetime', 'is_active' => 'boolean' ]; }
3. Basic Usage
// Create new record $user = new User(); $user->name = 'John Doe'; $user->email = 'john@example.com'; $user->save(); // Or using mass assignment $user = User::create([ 'name' => 'Jane Doe', 'email' => 'jane@example.com' ]); // Find records $user = User::find(1); $users = User::all(); $activeUsers = User::where('is_active', true)->get(); // Update records $user = User::find(1); $user->name = 'Updated Name'; $user->save(); // Delete records $user = User::find(1); $user->delete(); // Query builder $users = User::query() ->select(['name', 'email']) ->where('is_active', '=', true) ->orderBy('created_at', 'desc') ->limit(10) ->get();
Advanced Features
Soft Deletes
class User extends Model { protected bool $softDeletes = true; } // Soft delete $user->delete(); // Sets deleted_at timestamp // Include soft deleted records $users = User::query()->withTrashed()->get(); // Only soft deleted records $deletedUsers = User::query()->onlyTrashed()->get(); // Restore soft deleted record $user->restore(); // Permanently delete $user->forceDelete();
Validation
class User extends Model { protected array $rules = [ 'name' => 'required|min:2|max:100', 'email' => 'required|email|unique:users', 'age' => 'numeric|min:18' ]; protected array $messages = [ 'email.unique' => 'This email address is already taken.', 'age.min' => 'You must be at least 18 years old.' ]; } // Validation happens automatically on save() $user = new User(); $user->name = 'J'; // Too short, will fail validation if (!$user->save()) { $errors = $user->getValidationErrors(); }
Event Hooks
class User extends Model { protected function onCreating() { $this->password = password_hash($this->password, PASSWORD_DEFAULT); } protected function onCreated() { // Send welcome email } protected function onUpdating() { // Log changes } }
Query Scopes
class User extends Model { public function scopeActive($query) { return $query->where('is_active', true); } public function scopeByRole($query, $role) { return $query->where('role', $role); } } // Usage $activeUsers = User::query()->active()->get(); $admins = User::query()->active()->byRole('admin')->get();
Attribute Casting
class User extends Model { protected array $casts = [ 'email_verified_at' => 'datetime', 'is_active' => 'boolean', 'metadata' => 'json', 'score' => 'float' ]; } // Automatic casting $user = User::find(1); $user->is_active; // Returns boolean true/false $user->email_verified_at; // Returns DateTime object $user->metadata; // Returns array from JSON
Database Schema Requirements
For full functionality, your tables should include these columns:
CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, email VARCHAR(255) UNIQUE NOT NULL, password VARCHAR(255), is_active BOOLEAN DEFAULT true, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, deleted_at TIMESTAMP NULL DEFAULT NULL );
Configuration Options
You can customize model behavior through properties:
class User extends Model { // Table name (auto-detected from class name if not set) protected static string $table = 'users'; // Primary key column protected string $primaryKey = 'id'; // Enable/disable timestamps protected bool $timestamps = true; // Custom timestamp column names protected string $createdAt = 'created_at'; protected string $updatedAt = 'updated_at'; // Enable soft deletes protected bool $softDeletes = true; protected string $deletedAt = 'deleted_at'; // Mass assignment protection protected array $fillable = ['name', 'email']; protected array $guarded = ['id', 'password']; // Hide attributes in serialization protected array $hidden = ['password']; protected array $visible = ['name', 'email']; }
Requirements
- PHP 7.4 or higher
- PDO extension
- MySQL database
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Author
Kelvin Kurniawan
- Email: kelvin@aksarastudio.tech
- GitHub: @kelvinkurniawan
kelvinkurniawan/lightorm 适用场景与选型建议
kelvinkurniawan/lightorm 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 08 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「orm」 「php」 「mysql」 「activerecord」 「query-builder」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 kelvinkurniawan/lightorm 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kelvinkurniawan/lightorm 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 kelvinkurniawan/lightorm 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Kinikit - PHP Application development framework MVC component
Store your language lines in the database, yaml or other sources
PHP Database ORM for Symfony1. Do NOT use for new projects: please move to a newest Symfony release and Doctrine2
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
A PSR-7 compatible library for making CRUD API endpoints
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-08-06