phpfui/orm
Composer 安装命令:
composer require phpfui/orm
包简介
A Fast, Lightweight and Minimal ORM for MySQL, SQLite, MariaDB and PostGre
关键字:
README 文档
README
PHPFUI\ORM a minimal Object Relational Mapper (ORM) for MySQL, MariaDB, PostGre and SQLite3
Why another PHP ORM? In writing minimal and fast websites, it was determined that existing PHP ORM solutions were overly complex. PHPFUI\ORM is a little more than 7K lines of code in 52 files. It is designed to have a minimal memory footprint and excellent execution times for most database needs.
PHPFUI\ORM demonstrates superior performance for both speed and memory usage verses other ORMs. This is proven by a comparison of PHPFUI\ORM to Other ORMs for different SQL implementations.
PHPFUI\ORM is not an attempt to write an abstraction around SQL as other ORMs do, rather it is a way to work with SQL that closely matches the semantics of SQL, with the power of PHP objects. It allows PHP to manipulate SQL queries without having to write SQL in plain text. This is very useful for queries generated via user interfaces where the user is given a lot of flexability in how a query is defined.
Features
- Active Records A fully type checked object interface and implement basic CRUD functionality.
- Active Tables Full table operations (select, update, insert and delete) including support for where, having, limits, ordering, grouping, joins and unions.
- Data Cursors Cursors implement iterable and Countable eliminating the need to read full arrays into memory.
- Validation Fully customizable and translatable backend validation.
- Virtual Fields Supports get and set semantics for any custom or calculated field such as Carbon dates.
- Migrations Simple migrations offer atomic up and down migrations.
- Relations Parent, children, one to one, many to many, and custom relationships.
- Transactions Object based transactions meaning exceptions will not leave an open transacton.
- Type Safe Prevents stupid type errors.
- Injection Safe Uses PDO placeholders and field sanitation to prevent injection attacks.
- Raw SQL Query Support Execute any valid SQL command.
- Multiple Database Support Work with multiple databases simultaneously.
- Multi-Vendor Support Built on PDO with support for MySQL, MariaDB, PostGre and SQLite.
Usage
Setup
$pdo = new \PHPFUI\ORM\PDOInstance($yourConnectionString); // perform any custom configuration settings needed on $pdo \PHPFUI\ORM::addConnection($pdo);
See some basic usage examples in scripts/examples.php
Active Record Example
$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 Example
$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"; }
Validation Example
$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"; } }
Migration Example
Migrations are atomic and can be run in groups or individually up or down.
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'); } }
Type Safety
Exceptions Supported
Exceptions are generated in the following conditions:
- Accessing field or offset that does not exist
- Deleting records without a where condition (can be overridden)
- Incorrect type for Operator (must be an array for IN for example)
- Passing an incorrect type as a primary key
- Invalid join type
- Joining on an invalid table
All of the above exceptions are programmer errors and strictly enforced. Empty queries are not considered errors. SQL may also return Exceptions if invalid fields are used.
Type Conversions
If you set a field to the wrong type, the library logs a warning then converts the type via the appropriate PHP cast.
Multiple Database Support
While this is primarily a single database ORM, you can switch databases at run time. Save the value from $connectionId = \PHPFUI\ORM::addConnection($pdo); and then call \PHPFUI\ORM::useConnection(connectionId); to switch. \PHPFUI\ORM::addConnection will set the current connection.
The programmer must make sure the proper database is currently selected when database reads or writes happen and that any primary keys are correctly handled.
Copy tables example:
// get the current connection to restore later $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 \PHPFUI\ORM\PDOInstance($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);
Documentation
- Setup
- Active Record
- Active Table
- Cursors
- Virtual Fields
- Migrations
- Validation
- Translations
- Transactions
- Miscellaneous
Full Class Documentation
License
PHPFUI is distributed under the MIT License.
PHP Versions
This library only supports modern versions of PHP which still receive security updates. While we would love to support PHP from the late Ming Dynasty, the advantages of modern PHP versions far out weigh quaint notions of backward compatibility. Time to upgrade.
phpfui/orm 适用场景与选型建议
phpfui/orm 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 308 次下载、GitHub Stars 达 9, 最近一次更新时间为 2023 年 04 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「orm」 「sql」 「mysql」 「sqlite」 「lightweight」 「object」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 phpfui/orm 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 phpfui/orm 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 phpfui/orm 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Kinikit - PHP Application development framework MVC component
PHP Database ORM for Symfony1. Do NOT use for new projects: please move to a newest Symfony release and Doctrine2
Query filtering in your frontend
Database/ORM-agnostic query construction helper
PeskyORM - annoying ORM that contains tons of exceptions and throws them when anything goes wrong
统计信息
- 总下载量: 308
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-04-04