phpfui / orm
针对 MySQL、SQLite 和 MariaDB 的最小化 ORM
Requires
- php: >=8.1 <8.4
- phpfui/translation: ^1.0
- psr/log: ^3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- nesbot/carbon: *
- phpfui/phpunit-syntax-coverage: ^1.0
- phpstan/phpstan: ^1.8
- phpunit/phpunit: <=11.99
- roave/security-advisories: dev-latest
This package is auto-updated.
Last update: 2024-09-11 14:53:41 UTC
README
PHPFUI\ORM 是一个针对 MySQL、MariaDB 和 SQLite3 的最小化对象关系映射器 (ORM)
为什么还需要另一个 PHP ORM?在编写最小化和快速网站时,发现现有的 PHP ORM 解决方案过于复杂。 PHPFUI\ORM 在 50 个文件中包含 6.5K 行代码。它旨在具有最小的内存占用和出色的执行时间,以满足大多数数据库需求。
性能 PHPFUI\ORM 与 Eloquent 在不同 SQL 实现中的比较。
PHPFUI\ORM 并非试图像其他 ORM 一样在 SQL 上编写抽象,而是一种与 SQL 语义紧密匹配的工作方式,同时具有 PHP 对象的强大功能。它允许 PHP 在不编写纯文本 SQL 的情况下操作 SQL 查询。这对于通过用户界面生成的查询非常有用,用户可以灵活地定义查询。
特性
- Active Records 完全类型检查的对象接口,并实现基本的 CRUD 功能。
- Active Tables 支持包括 WHERE、HAVING、LIMITS、ORDER BY、GROUP BY、JOIN 和 UNION 的完整表操作(选择、更新、插入和删除)。
- Data Cursors 游标实现 iterable 和 Countable,消除了将完整数组读入内存的需要。
- Validation 完全可定制和可翻译的后端验证。
- Virtual Fields 支持获取和设置任何自定义或计算字段(如 Carbon 日期)的语义。
- Migrations 简单的迁移提供原子化的上移和下移迁移。
- Relations 父、子、一对一、多对多和自定义关系。
- Transactions 基于对象的交易意味着异常不会留下未完成的交易。
- Type Safe 防止愚蠢的类型错误。
- Injection Safe 使用 PDO 占位符和字段清理来防止注入攻击。
- 原始 SQL 查询支持 执行任何有效的 SQL 命令。
- 多数据库支持 同时处理多个数据库。
- 多厂商支持 基于 PDO,支持 MySQL、MariaDB 和 SQLite。
使用方法
设置
$pdo = new \PHPFUI\ORM\PDOInstance($yourConnectionString); // permform any custom configuration settings needed on $pdo \PHPFUI\ORM::addConnection($pdo);
Active Record 示例
$book = new \App\Record\Book(); $book->title = 'PHP ORM: The Right Way'; $book->price = 24.99; $author = new \App\Record\Author(); $author->name = 'Bruce Wells'; $book->author = $author; // Save the author $book->save(); // Save the book
Active Table 示例
$bookTable = new \App\Table\Book(); $bookTable->setWhere(new \PHPFUI\ORM\Condition('title', '%orm%', new \PHPFUI\ORM\Operator\Like())); $bookTable->join('author'); foreach ($bookTable->getDataObjectCursor() as $book) { echo "{$book->title} by {$book->name} is $ {$book->price}\n"; } // discount all PHP books to 19.99 $bookTableUpdater = new \App\Table\Book(); $bookTableUpdater->setWhere(new \PHPFUI\ORM\Condition('title', '%PHP%', new \PHPFUI\ORM\Operator\Like())); $bookTableUpdater->update(['price' => 19.99]); foreach ($bookTableUpdater->getRecordCursor() as $book) { echo "{$book->title} by {$book->author->name} is now $ {$book->price}\n"; }
验证示例
$book->title = 'This title is way to long for the database and will return a validation error. We should write a migration to make it varchar(255)!'; $errors = $book->validate(); foreach ($errors as $field => $fieldErrors) { echo "Field {$field} has the following errors:\n"; foreach ($fieldErrors as $error) { echo $error . "\n"; } }
迁移示例
迁移是原子的,可以按组或单独上移或下移执行。
namespace App\Migration; class Migration_1 extends \PHPFUI\ORM\Migration { public function description() : string { return 'Lengthen book.title field to 255'; } public function up() : bool { return $this->alterColumn('book', 'title', 'varchar(255) not null'); } public function down() : bool { return $this->alterColumn('book', 'title', 'varchar(50) not null'); } }
类型安全
支持的异常
以下情况下会生成异常
- 访问不存在字段或偏移量
- 没有 WHERE 条件删除记录(可以覆盖)
- 运算符类型不正确(例如,IN 必须是数组)
- 传递错误的类型作为主键
- 无效的连接类型
- 在无效的表上连接
上述所有异常都是程序员错误,并被严格执行。空查询不被视为错误。如果使用无效的字段,SQL 也可能返回 异常。
类型转换
如果您将字段设置为错误类型,库会记录警告,然后通过适当的 PHP 转换转换类型。
多数据库支持
虽然这是一个主要的单数据库ORM,但在运行时可以切换数据库。保存从 $connectionId = \PHPFUI\ORM::addConnection($pdo);
获取的值,然后调用 \PHPFUI\ORM::useConnection($db);
来切换。 \PHPFUI\ORM::addConnection
将设置当前连接。
程序员必须确保在数据库读取或写入操作时已选择正确的数据库,并且任何主键都得到正确处理。
复制表示例
// get the current connection just for fun $currentConnection = \PHPFUI\ORM::getConnection(); $cursors = []; // getRecordCursor will bind the cursor to the current database instance $cursors[] = (new \App\Table\Author())->getRecordCursor(); $cursors[] = (new \App\Table\Book())->getRecordCursor(); // set up a new database connection $pdo = new \PDO($newConnectionString); $newConnectionId = \PHPFUI\ORM::addConnection($pdo); foreach ($cursors as $cursor) { foreach ($cursor as $record) { $record->insert(); // insert into new database ($newConnectionId) } } // back to the original database \PHPFUI\ORM::useConnection($currentConnection);
文档
完整类文档
许可证
PHPFUI 在MIT许可证下分发。
PHP版本
此库仅支持 现代 的PHP版本,这些版本仍在接收安全更新。虽然我们愿意支持明末的PHP版本,但现代PHP版本的优势远超过对向后兼容性的陈旧观念。是时候升级了。