dragonmantank / casket
一个紧凑的PHP对象关系管理库
Requires
- php: >=5.3.0
- aura/sql: ~2.0
- aura/sqlquery: ~2.0
Requires (Dev)
- phpunit/phpunit: 4.0.*
This package is auto-updated.
Last update: 2024-09-21 20:25:51 UTC
README
最后更新:2014-05-02,Chris Tankersley
Casket是一个用于PHP的紧凑型对象关系管理库。它允许快速原型设计和管理需要持久化的对象。Casket是基于我之前创建的旧ORM PhpORM的一个分支。您可以在https://github.com/dragonmantank/PhpORM找到它
由于持久化不总是意味着到数据库,它很容易扩展以支持不同的持久化层。
这个版本使用Aura.SQL提供对任何Aura.SQL支持的数据库提供者的支持。您只需要PDO!
Casket的组成部分
- 实体 - 单个对象
- 仓库 - 允许搜索和检索
- 存储 - 访问不同数据存储的方式
实体
实体是应用程序中的“事物”。如果您正在构建一个宠物网站,实体可能是“动物”、“收养机构”、“地址”等。实体是需要持久化的单个对象,通常存储在数据库中。
实体可能看起来像这样
Class Animal
{
public $id; // Database ID
public $type; // Type of animal
public $inductionDate // Date Animal came to Shelter
public $name; // Name of the animal
public $shelter_id // Foreign key of the animal shelter
}
然后您可以像这样访问实体
$animal = new Animal();
$animal->type = 'Cat';
$animal->inductionDate = time();
$animalRepository->save($animal);
仓库
仓库是获取数据存储信息的推荐方式。我们已经消除了使用直接数据访问对象的需求。仓库通常围绕特定的实体设置,并且仅与该实体一起工作。这允许仓库根据数据源自动生成实体。
对于数据库支持的持久化,基本仓库不需要特定的类,可以利用提供的DBRepository类。它只需要一种存储机制和一个原型对象来基于其结果。
// $storage is a pre-created connection to your data store, see the Storage section
$animalRepo = new Casket\Repository\DBRepository($storage, new Animal());
这个仓库将返回动物实体(或动物实体的集合)
$cats = $animalRepo->fetchAllBy(array('type' => 'cat')); // Return all the cats
$dog = $repo->find($dogId); // Find the specified object by primary key
$fluffy = $repo->findBy(array('name' => 'Fluffy')); // Search by something other than primary key
存储
存储系统提供了一种与某种存储系统(无论是数据库还是API)交互的标准方式。Casket提供了一个基于PDO的存储系统,该系统利用Aura.SQL和Aura.SQL-Query提供对许多不同数据库后端的访问。
对于AuraExtendedPdo存储系统,您只需用AuraExtendedPdo对象和Aura.SQL-Query的QueryFactory调用它。
$storage = new Casket\Storage\AuraExtendedPdo($extendedPdo, new Aura\SqlQuery\QueryFactory('mysql'));
然后您可以使用存储与您的仓库一起使用,以便它们可以访问数据
$animalRepo = new Casket\Repository\DBRepository($storage, new Animal());