rawebone / ormish
轻量级 ORM
v0.2.0
2014-03-11 19:10 UTC
Requires
- php: >=5.3
- phpdocumentor/reflection-docblock: 2.0.1
- psr/log: *
Requires (Dev)
- phpspec/phpspec: 2.0.x-dev
This package is not auto-updated.
Last update: 2024-09-24 01:00:59 UTC
README
Ormish 是一个注重简单的对象关系映射器。它依赖于一定的扩展和遵守数据库规范。目前有一个通用的 SQL 连接器,以实现最大兼容性。
该库旨在提供一种活动记录模式来访问数据库,具有基本语法以供一般使用,并能够轻松扩展以满足您的需求。我主要写这个是为了学习和自己使用,除非有其他方面的显著兴趣。
注意:这是一个正在进行中的项目
安装
安装通过 Composer 完成
{
"require": {
"rawebone/ormish": "dev-master"
}
}
使用
ORM 很容易上手
<?php use Rawebone\Ormish\Table; use Rawebone\Ormish\Factory; // We use the factory to build a connection $factory = new Factory("sqlite::memory:", "", ""); $orm = $factory->build(); // We then "Attach" tables to the container - these are used to provide the // information we need to connect tables to classes and give options for // the way SQL should be generated. $orm->attach(new Table('My\Models\Entity', "data_table")); // We can then access the database table via it's name, which returns a gateway // object. This allows us to perform actions such as finding data on our table // using the options from the Table object. $gateway = $orm->data_table(); $gateway = $orm->get("data_table"); // We can then find a record $model = $gateway->find(1); // Data is accessed by properties $model->name = "Barry"; // Relationships are accessed by methods $model->sons(); // Sons[] // We can save or delete the model directly $model->save(); $model->delete(); // Creates a new instance of the Model for our table // An array of initial data can be passed in this call to populate the model. $fresh = $gateway->create(array("name" => "john"));
实体的标记也很直观
namespace My\Models; use Rawebone\Ormish\Entity as BaseEntity; /** * Given that we do not have "real" methods and properties to provide an * interface, we can use the annotations (courtesy of ApiGen) for your IDE, * if it supports them: * * @property int $id * @property string $name * @property string $complex_name; * @method \My\Models\Other sons() */ class Entity extends BaseEntity { public function sons() { return $this->getDatabase()->findWhere("parent_id = ?", $this->id); } }
与其它系统相比,这需要更多的模板代码,但回报是您有一个简单的机制来确保一切正常。
事务
默认情况下,数据库操作不在事务中执行,但可以通过使用 Transaction 对象来实现。
use Rawebone\Ormish\Transaction; $tran = new Transaction($orm->getExecutor(), function () use ($entity) { $entity->save(); $entity->delete(); }); $tran->run();
贡献
命名空间
根命名空间 Rawebone\Ormish 保留用于直接对最终用户可见的对象(如数据库、表、实体),以便于使用 API。
Rawbone\Ormish\Actions 命名空间用于可以应用于 Gateway 对象的操作(例如:$table->find(1) 将引用 Finder 操作)。目标是创建包含与数据库交互所需逻辑的小对象,以干净简单的方式工作。
Rawbone\Ormish\Utilities 命名空间用于在 API 内部消耗的对象,对用户没有外部价值。例如,最终用户不需要看到 Shadow 或 NullShadow 对象即可使用 API。
路线图
对于版本 0.2.0
- 完全实现
Actions并将其从Gateway对象中删除。(100%) - 将对象完全移入
Utilities命名空间,这些对象对最终用户没有价值。(90%) - 将错误转换为异常模型。(100%)
对于版本 0.3.0
- 实现类型映射系统。这将是一个破坏性的变更,因为需要为实体添加额外的注解。
- 为表实现注解,并弃用手动附加表。这将有助于将实体保持与数据库的紧密关系。
- 以类似
Actions的方式实现Relationship处理。
对于版本 0.4.0
- 实现结果和可能配置的缓存(希望通过 PSR)。
- 基本的模式操作
测试
所有测试都在 PhpSpec 下运行。
许可
MIT 许可证。随意使用。