定制 iescarro/lekoi-model 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

iescarro/lekoi-model

Composer 安装命令:

composer require iescarro/lekoi-model

包简介

A lightweight ORM and database abstraction layer for the Lekoi PHP framework.

README 文档

README

Lekoi Framework Logo

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 Model class support (if you add it) for cleaner domain models
  • Lightweight and minimal dependencies (just php-util currently)

📦 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 我们能提供哪些服务?
定制开发 / 二次开发

基于 iescarro/lekoi-model 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 14
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 19
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-10-12