theopenweb/lessql

LessQL:一个轻量级且高性能的PHP ORM替代方案

dev-master 2024-07-14 14:24 UTC

This package is not auto-updated.

Last update: 2024-09-23 13:32:17 UTC


README

LessQL是PHP对象关系映射的一个轻量级且高性能的替代方案。

指南 | 约定 | API参考 | 关于

如果您正在寻找一个比原始PDO更好的基于SQL的方法,请考虑DOP作为替代方案。

分支说明

  • 本节中的注释概述了与原始仓库morris/lessql的差异信息
  • 与其他部分相比,其他部分可能只有重要的/适当的更新。
  • 分支原因
    • 最后更新过旧
    • 与较新版本的PHP版本使用时出现错误/警告
      • PHP 8.1弃用错误,如“offsetExists”。
      • 找不到行异常。通过require_once修复Row。
    • 仍在一些项目中使用它,因此需要有一个可工作的最新源代码(通过packagist的composer安装)。
  • 注释
    • 提到了morris/dop作为替代方案,但它似乎没有多少开发或维护。

安装

使用composer安装LessQL:composer require theopenweb/lessql。LessQL需要PHP >= 8.0和PDO。

用法

// SCHEMA
// user: id, name
// post: id, title, body, date_published, is_published, user_id
// categorization: category_id, post_id
// category: id, title

// Connection
$pdo = new PDO('sqlite:blog.sqlite3');
$db = new LessQL\Database($pdo);

// Find posts, their authors and categories efficiently:
// Eager loading of references happens automatically.
// This example only needs FOUR queries, one for each table.
$posts = $db->post()
    ->where('is_published', 1)
    ->orderBy('date_published', 'DESC');

foreach ($posts as $post) {
    $author = $post->user()->fetch();

    foreach ($post->categorizationList()->category() as $category) {
        // ...
    }
}

// Saving complex structures is easy
$row = $db->createRow('post', [
    'title' => 'News',
    'body' => 'Yay!',
    'categorizationList' => [
        [
            'category' => ['title' => 'New Category']
        ],
        ['category' => $existingCategoryRow]
    ]
]);

// Creates a post, a new category, two new categorizations
// and connects them all correctly.
$row->save();

特性

  • 通过智能预加载进行高效的深度查找
  • 查询数量恒定,无N+1问题
  • 使用一个方法调用保存复杂的嵌套结构
  • 约定优于配置
  • 与数据库紧密工作:LessQL不是一个ORM
  • 不需要胶水代码
  • 干净、易于阅读的源代码
  • 已完全测试SQLite3、MySQL和PostgreSQL
  • MIT许可证

主要受到NotORM的启发,它从头开始编写,以提供干净的API和简化的概念。

贡献者

感谢!