morris/lessql

LessQL:一个轻量级且性能优良的PHP ORM替代品

1.0.0 2020-05-09 16:42 UTC

This package is auto-updated.

Last update: 2024-09-15 17:26:58 UTC


README

Build Status Test Coverage

LessQL是PHP的Object-Relational Mapping的一个轻量级且性能优良的替代品。

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

如果你在寻找一种优于原始PDO的基于SQL的方法,请检查DOP作为替代方案。

安装

通过composer安装LessQL:composer require morris/lessql。LessQL需要PHP >= 5.6和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和简化的概念。

贡献者

谢谢!