mnavarrocarter/sqlx
Composer 安装命令:
composer require mnavarrocarter/sqlx
包简介
Simple, low-overhead relational database toolkit for modern PHP applications
README 文档
README
Simple, low-overhead relational database toolkit for modern PHP applications.
Quick Start
You can map any of your classes as an entity just marking with the SQLX\Entity attribute and
marking the id fields with the SQLX\Id attributes. All the other stuff can be guessed at runtime.
Of course, you can also be explicit by passing the SQLX\Field annotation.
<?php use MNC\SQLX\Engine\Metadata as SQLX; #[SQLX\Entity] class Account { #[SQLX\Id] private int $id; #[SQLX\Field('user_name')] private string $username; private string $email; private string $password; private \DateTimeImmutable $createdAt; public function __construct(string $username, string $email, string $password) { $this->username = $username; $this->email = $email; $this->password = $password; $this->createdAt = new DateTimeImmutable(); } public function getId(): int { return $this->id; } public function changePassword(string $password): void { $this->password = $newPassword; } }
Then, in your bootstrapping code, you can initialize the engine. Here we configure it with a simple
PDOWrapper connection instance. We also add a Namer strategy to cast all properties without
explicit column names to underscore. So createdAt property will be mapped to the created_at
column.
<?php use MNC\SQLX\SQL\Connection\PDOWrapper; use MNC\SQLX\Engine; use Castor\Context; $conn = PDOWrapper::from(new PDO('sqlite::memory')); $engine = Engine::configure($conn) ->withNamer(new Engine\Namer\Underscore()) ->build() ;
Once you have the engine bootstrapped, is easy to inject it into your services and use its public api. This is the full public api at the moment. Every other detail is considered internal.
$ctx = Context\nil(); // You can work with your objects in the domain layer. $account = new Account('jdoe', 'jdoe@example.com', 'secret'); // Persisting a new object causes an insert: // INSERT INTO account (user_name, email, password, created_at) VALUES ('jdoe', 'jdoe@example.com', 'secret') $engine->persist($ctx, $account); // Upon insertion, we can fetch the last inserted id. // We grab it automatically for you if the driver supports it. echo $account->getId(); // (int) 1 // Is easy to find records: // SELECT FROM account * WHERE id = 1; $account = $engine->find($ctx, Account::class)->andWhere('id = ?', 1)->one(); $account->changePassword('secret2'); // Persisting an existing or "known" object causes an update: // UPDATE account SET user_name = 'jdoe', email = 'jdoe@example.com', password = 'secret2' WHERE id = 1 $engine->persist($ctx, $account); // You can delete an object of course // DELETE FROM account WHERE id = 1 $engine->delete($ctx, $account);
NOTE: All queries are correctly escaped and parametrized.
Caveats
This is not an ORM
This is not an Object Relational Mapper, because it does not do relationships. They are unsupported and not planned for the moment. It would say is an Object Mapper: maps objects from the records in your database, but even that is far-fetched. I prefer the term "database toolkit".
Proper relationship support is one of the biggest factors in making an ORM complex. Tracking lifecycle of nested objects, detecting their changes and other things related to relationships can incur in a tremendous performance penalty. Moreover, relations are overrated: there are not needed in the majority of cases and often bite inexperienced developers with all sorts of bugs (N+1 and bi-directional associations).
Even Doctrine best practices hint that unnecessary relationships should be avoided, and lists a few other topics where the constraints imposed by relationships can affect performance.
Therefore, I'm avoiding full relationship support at the moment.
Needs more testing
Although the codebase is fairly tested and critical routines are well covered, I still need write more test cases in different drivers, with different queries and edge cases.
Building up a mature test suite like that takes time, but if you are interested in improving
support for a particular Driver or Engine, I would happily take a PR. The FunctionalTestCase has
all you need to set up a connection and start testing against a particular engine. I'm interested
mostly in how the database receives certain data types (dates, blobs), handling of reserved keywords,
identifier quoting and other things. And, of course, the main api of find, persist and delete needs
to be working too.
mnavarrocarter/sqlx 适用场景与选型建议
mnavarrocarter/sqlx 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 1, 最近一次更新时间为 2022 年 10 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 mnavarrocarter/sqlx 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mnavarrocarter/sqlx 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2022-10-31