sras / entity-builder
Requires
- php: >=5.5.0
This package is not auto-updated.
Last update: 2020-08-17 10:15:30 UTC
README
从查询结果中创建完整的聚合实体。
这是一个从简单查询或连接查询的结果中构建聚合根对象的库。
通过 composer 安装
{
"require":{"sras/entity-builder": "dev-default"}
}
请参阅下面的部分以了解实际用法。
工作原理
它通过接受一组规则来工作,这些规则定义了结果的结构。它使用这些规则来填充实体对象并将它们连接起来。规则定义了:
- 用于识别实体列的前缀。
- 实体的实例别名。
- 用于连接它们的实体中的方法。
- 用于识别特定实体的行的主键。
- 返回给调用者的实体。
它扫描每一行并使用前缀分离每个实体的数据。然后,它通过在去除下划线并将剩余部分驼峰命名后添加 'set' 来将列名转换为设置方法名。然后,它使用别名作为键将实体分配给数组的元素,并使用规则和方法将它们连接起来。当完成特定实体的所有行后,它向调用代码返回完全填充的实体。
例如,假设我们有一个查询结果的数组,如下所示。
<?php
$result = array(
array('u_id'=>1, 'u_name'=>'sandeep','u_country_id'=>33,'p_pid'=>23,'p_title'=>'post_1','c_id'=>33,'c_name'=>'india'),
array('u_id'=>1, 'u_name'=>'sandeep','u_country_id'=>33,'p_pid'=>34,'p_title'=>'post_2','c_id'=>33,'c_name'=>'india'),
array('u_id'=>1, 'u_name'=>'sandeep','u_country_id'=>33,'p_pid'=>4,'p_title'=>'post_5','c_id'=>33,'c_name'=>'india'),
array('u_id'=>2, 'u_name'=>'max','u_country_id'=>34,'p_pid'=>6,'p_title'=>'post_6','c_id'=>34,'c_name'=>'japan'),
array('u_id'=>2, 'u_name'=>'max','u_country_id'=>34,'p_pid'=>6,'p_title'=>'post_6','c_id'=>34,'c_name'=>'japan'),
);
此结果包含来自三个表(用户、帖子和国家)的行。我们的目标是将此数组转换为两个用户对象,其中包含已填充的 'Post' 和 'Country' 实体。我们使用以下规则来初始化构建器对象。
<?php
$rules = array( 'columns for \Test\User u1 prefixed by u_',
'columns for \Test\Post p1 prefixed by p_',
'columns for \Test\Country c1 prefixed by c_',
'add p1 to u1 using addPost',
'add u1 to p1 using setUser',
'add c1 to u1 using setCountry',
'primary key for \Test\User is id',
'primary key for \Test\Post is pid',
'return u1',
);
您可以使用以下代码获取两个用户实体。
<?php
$eb = \sras\EntityBuilder::getInstance($rules); // initialize the builder with the rules
$eb->setResult($result); // sets the result to be interpreted. It can be an array or any iterable object, like a pdo statement.
$entities = $eb->getAll(); // Convert all the data in the result set into entities, based on the rules.
echo count($entities); //outputs 2
echo $entities[0]->getCountry()->getName(); // outputs 'india'
遍历结果
您可以通过调用 getIterator()(而不是 getAll())来获取迭代器,而不是将整个结果集转换为实体。迭代器将(使用生成器)在完成构建一个实体时提供完全填充的实体。如果您有一个大的 PDO 语句结果,您可以从其中逐个获取填充良好的实体。这不会需要太多内存,因为结果中的所有实体不是一次性构建的。相反,当它完成处理单个实体的行时,它会提供它并等待下一次迭代来构建下一个实体。
<?php
$eb = \sras\EntityBuilder::getInstance($rules); // initialize the builder with the rules
$eb->setResult($result); // sets the result to be interpreted. It can be an array or any iterable object, like a pdo statement.
// Get User entities as they are built by the builder
foreach($eb->getIterator() as $user) {
echo $user->getCountry()->getName(); // prints 'india', 'japan' in two iterations
}
标识映射。
构建器维护一个标识映射,该映射在所有实体之间共享,这些实体是由构建器返回的。这意味着即使它来自结果的各个部分,您也会获得相同对象的单个实例。这意味着如果您通过构建器返回具有 ID 10 的用户实体并更改其属性,则在未来的调用中遇到 ID 为 10 的用户时,它将始终返回相同的用户实例。请参阅以下代码部分。
<?php
$eb = \sras\EntityBuilder::getInstance($rules); // initialize the builder with the rules
$eb->setResult($result); // sets the result to be interpreted. It can be an array or any iterable object, like a pdo statement.
$entities = $eb->getAll(); // Convert all the data in the result set into entities, based on the rules.
echo $entities[0]->getCountry()->getName(); // outputs 'india'
$entities[0]->getCountry()->setName('INDIA');
//Build the entities from result again
$entities = $eb->getAll(); // Convert all the data in the result again
echo $entities[0]->getCountry()->getName(); // outputs 'INDIA', instead of 'india'.