在zend框架中检索数据库行的每个字段作为对象2 [英] Retrieving every field of a database row as object in zend framework 2

查看:175
本文介绍了在zend框架中检索数据库行的每个字段作为对象2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我们有结果集,以获取一个行作为对象但我如何获得每个字段作为一个单独的对象?考虑此数据库行:

  user_id address_id product_id shop_id 
5 3 134 2



我要检索并保存行如下:

  userEntity AddressEntity ProductEntity ShopEntity 


解决方案

不是如何应该使用 TableDataGateway ,因为你正在寻找的是更复杂的功能,如Doctrine 2 ORM和类似的数据映射器。 p>

这里是一个可能的问题解决方案,它涉及使用 custom hydrator docs )。我的例子是简化的,但我希望它澄清你应该如何构建你的结果集。



首先,定义你的实体(我简化示例假设 UserEntity 是您的水合作用的根):

  class UserEntity {
/ * fields为了简单示例* /
public $ address;
public $ product;
public $ shop;
}
class AddressEntity {/ *为了简单,在这里添加公共字段* /}
class ProductEntity {/ *为了简单,在此添加公共字段* /}
class ShopEntity {/ *这里添加公共字段为简单起见* /}

然后,构建特定于单个实体的hydrators: p>

 使用Zend \Stdlib \Hydrator\HydratorInterface作为Hydrator; 

class AddressHydrator implements Hydrator {
// @TODO:implementation up to you
}

class ProductHydrator实现Hydrator {
// @TODO:执行到你
}

class ShopHydrator实现Hydrator {
// @TODO:执行到你
}

然后我们将这些水化器聚合成一个专门用于水化 UserEntity

  class UserHydrator extends \Zend\Stdlib\Hydrator\ObjectProperty {
public function __construct(
Hydrator $ addressHydrator,
Hydrator $ productHydrator,
Hydrator $ shopHydrator
){
$ this-> addressHydrator = $ addressHydrator;
$ this-> productHydrator = $ productHydrator;
$ this-> shopHydrator = $ shopHydrator;
}

public function hydrate(array $ data,$ object)
{
if(isset($ data ['address_id'])){
$ data ['address'] = $ this-> addressHydrator-> hydrate($ data,new AddressEntity());
}

if(isset($ data ['product_id'])){
$ data ['product'] = $ this-> productHydrator-> hydrate $ data,new ProductEntity());
}

if(isset($ data ['shop_id'])){
$ data ['shop'] = $ this-> shopHydrator-> hydrate $ data,new ShopEntity());
}

return parent :: hydrate($ data,$ object);
}
}

现在您可以使用它来处理结果集。让我们为 UserEntityTableGateway 定义服务:

 'UserEntityTableGateway'=> function($ sm){
$ dbAdapter = $ sm-> get('Zend\Db\Adapter\Adapter');
$ resultSetPrototype = new ResultSet();

$ resultSetPrototype-> setArrayObjectPrototype(new UserHydrator());

返回新的TableGateway('user',$ dbAdapter,null,$ resultSetPrototype);
},



这些都是简化的例子,



您还可以查看文档中有关 Aggregate Hydrator 水化策略,专为解决您的问题而设计。


I know we have result set to get a row as object But How can I get every field as a separate object ? consider of this database row :

user_id     address_id  product_id  shop_id
5               3       134          2

I want to retrieve and save the row as follows :

userEntity  AddressEntity   ProductEntity   ShopEntity

解决方案

This is not how the TableDataGateway is supposed to be used, since what you are looking for are more complex features such as the ones of Doctrine 2 ORM and similar data-mappers.

Here is one possible solution to the problem, which involves using a custom hydrator (docs). My example is simplified, but I hope it clarifies how you are supposed to build your resultset.

First, define your entities (I'm simplifying the example assuming that UserEntity is the root of your hydration):

class UserEntity {
    /* fields public for simplicity of the example */
    public $address;
    public $product;
    public $shop;
}
class AddressEntity { /* add public fields here for simplicity */ }
class ProductEntity { /* add public fields here for simplicity */ }
class ShopEntity { /* add public fields here for simplicity */ }

Then, build hydrators specific for the single entities:

use Zend\Stdlib\Hydrator\HydratorInterface as Hydrator;

class AddressHydrator implements Hydrator {
    // @TODO: implementation up to you
}

class ProductHydrator implements Hydrator {
    // @TODO: implementation up to you
}

class ShopHydrator implements Hydrator {
    // @TODO: implementation up to you
}

Then we aggregate these hydrators into one that is specifically built to hydrate a UserEntity:

class UserHydrator extends \Zend\Stdlib\Hydrator\ObjectProperty {
    public function __construct(
        Hydrator $addressHydrator,
        Hydrator $productHydrator,
        Hydrator $shopHydrator
    ) {
        $this->addressHydrator = $addressHydrator;
        $this->productHydrator = $productHydrator;
        $this->shopHydrator    = $shopHydrator;
    }

    public function hydrate(array $data, $object)
    {
        if (isset($data['address_id'])) {
            $data['address'] = $this->addressHydrator->hydrate($data, new AddressEntity());
        }

        if (isset($data['product_id'])) {
            $data['product'] = $this->productHydrator->hydrate($data, new ProductEntity());
        }

        if (isset($data['shop_id'])) {
            $data['shop'] = $this->shopHydrator->hydrate($data, new ShopEntity());
        }

        return parent::hydrate($data, $object);
    }
}

Now you can use it to work with your resultset. Let's define the service for your UserEntityTableGateway:

 'UserEntityTableGateway' => function ($sm) {
     $dbAdapter          = $sm->get('Zend\Db\Adapter\Adapter');
     $resultSetPrototype = new ResultSet();

     $resultSetPrototype->setArrayObjectPrototype(new UserHydrator());

     return new TableGateway('user', $dbAdapter, null, $resultSetPrototype);
 },

These are all simplified examples, but they should help you understanding how powerful hydrators can be, and how you can compose them to solve complex problems.

You may also check the chapters in the documentation about the Aggregate Hydrator and Hydration Strategies, which were designed specifically to solve your problem.

这篇关于在zend框架中检索数据库行的每个字段作为对象2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆