iescarro/lekoi-model
Composer 安装命令:
composer require iescarro/lekoi-model
包简介
A lightweight ORM and database abstraction layer for the Lekoi PHP framework.
README 文档
README
A lightweight database abstraction / ORM-like model component for the Lekoi PHP framework.
Supports MySQL (mysqli) and SQLite3 through a consistent interface, and provides a simple DB facade and base Model capabilities.
🚀 Features
- Simple CRUD operations:
insert(),update(),delete(),get(),result(),row() - Works with both MySQL (
mysqli) and SQLite3 with one unified API - DB facade (
DB::…) for convenient static access - Base
Modelclass support (if you add it) for cleaner domain models - Lightweight and minimal dependencies (just
php-utilcurrently)
📦 Installation
Via Composer:
composer require iescarro/lekoi-model
Then in your project’s composer.json, you’ll get Lekoi\ namespace autoloaded pointing to the src/ directory.
📁 Directory Structure (Recommended)
lekoi-model/ ├── src/ │ ├── Database/ │ │ ├── MySQLDatabase.php │ │ ├── SQLiteDatabase.php │ │ └── IDatabase.php │ ├── DB.php │ └── Model.php ├── tests/ │ └── (unit tests) ├── .env.example ├── composer.json └── README.md
🧰 Usage
1. Configure DB Facade
Before using DB, initialize it with your connection settings (e.g. in your app’s bootstrap or index.php):
DB::init([ 'driver' => 'mysqli', // or 'sqlite3' 'host' => getenv('DB_HOST'), 'port' => getenv('DB_PORT'), 'dbname' => getenv('DB_DATABASE'), 'username' => getenv('DB_USERNAME'), 'password' => getenv('DB_PASSWORD'), ]);
For SQLite you might do:
DB::init([ 'driver' => 'sqlite3', 'dbname' => __DIR__ . '/data/database.sqlite', ]);
2. CRUD Examples
// Insert DB::insert('users', [ 'name' => 'Alice', 'email' => 'alice@example.com', ]); // Update DB::update( 'users', ['email' => 'alice.new@example.com'], ['id' => 1] ); // Delete DB::delete('users', [ 'id' => 1 ]); // Get rows $rows = DB::get('users', ['status' => 'active'])->result(); foreach ($rows as $row) { echo $row['name'] . PHP_EOL; } // Get single row (if you define row()) $user = DB::get('users', ['id' => 2])->row();
🧬 (Optional) Model Extension
You can build a base Model class to streamline usage in your domain classes:
class Model { protected $table; protected $primaryKey = 'id'; protected $attributes = []; public function __construct(array $attrs = []) { $this->attributes = $attrs; } public function save(): bool { if (isset($this->attributes[$this->primaryKey])) { return DB::update( $this->table, $this->attributes, [$this->primaryKey => $this->attributes[$this->primaryKey]] ); } else { return DB::insert($this->table, $this->attributes); } } public static function find(int $id): ?self { $row = DB::get(static::$table, [static::$primaryKey => $id])->row(); return $row ? new static($row) : null; } }
Then your domain model:
class User extends Model { protected $table = 'users'; protected $primaryKey = 'id'; // optionally define fillable or guarded etc. }
Usage:
$user = User::find(1); $user->name = 'Bob'; $user->save(); $users = DB::get('users')->result();
🧪 Testing
You can add unit tests (e.g. using PHPUnit) under tests/ to ensure your DB and Database classes behave as expected across both drivers. Add the test dependency to composer.json under require-dev.
Run tests via:
composer test
⚙️ Configuration & Best Practices
- Validate table & column names (e.g. alphanumeric + underscores) to avoid SQL injection risks.
- Use environment variables or a config file to keep credentials outside version control.
- Consider connection pooling or persistent connections for production use.
- Log queries (e.g. via a queryLog() method) to help debugging.
- Extend support for more drivers (PostgreSQL, SQL Server, etc.) by implementing new driver classes that adhere to IDatabase.
📜 License
This project is licensed under the MIT License — see the LICENSE file for details.
💡 Contribution
Contributions, issues, and feature requests are welcome! Please feel free to:
- Open a bug report or feature request
- Submit pull requests
- Add tests for new features or drivers
🧷 References & Inspiration
- CodeIgniter 3’s Model / Database pattern
- Laravel’s Eloquent / DB facade design
- Doctrine / other ORMs as inspiration for future expansion
iescarro/lekoi-model 适用场景与选型建议
iescarro/lekoi-model 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 10 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「framework」 「database」 「orm」 「php」 「sqlite」 「mysqli」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 iescarro/lekoi-model 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 iescarro/lekoi-model 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 iescarro/lekoi-model 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Framework HLEB2 is the foundation of the web application. Provides ease of development and application performance.
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.
统计信息
- 总下载量: 14
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-12
