如何获取Doctrine2结果对象作为关联数组? [英] How to get a Doctrine2 result object as an associative array?

查看:287
本文介绍了如何获取Doctrine2结果对象作为关联数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的实体,它是一个包含我的用户数据
的表,我想以特定用户的形式获取数组的所有列,然后json_encode,但是我得到的是一个实体对象为每个值使用get方法。我只想要一个关联数组的用户表值。
我尝试并没有工作的代码(返回的实体对象)如下:
1。

  $ qb = $ this-> em-> createQueryBuilder(); 
$ qb-> add('select','a')
- > add('from','Entities\Adminprofile a')
- > add(' where','a.userid = 3333');
$ accounts = $ qb-> getQuery() - > getResult();

2。

 code> $ account = $ this-> em-> getRepository('Entities\Adminprofile') - > findOneBy(
array('userid'=>'3333'));

PS:im使用z2d2 Project,这是将doctrine2集成到Zend框架中。

解决方案

当您执行 $ accounts = $ qb-> getQuery() - > getResult(); 你传递给getResult的参数告诉它如何水平将返回的结果集。



数组水合



如果你想要数组, code> CONSTANT 用于数组水合 Doctrine\ORM\Query :: HYDRATE_ARRAY


$ accounts = $ qb-> getQuery() - > getResult(Doctrine\ORM\Query :: HYDRATE_ARRAY) p>

如果您使用 findOneBy(),那么它将始终<强>返回一个实体。由于内部如何找到作品,你不能通过除了返回实体之外的任何其他方式来使其水化。



在这种情况下,您需要做的是在您的实体内创建一个 getValues()您的实体数组,如下所示:

  public function getSimpleValues(){
return array(
'id'=> $ this-> getId(),
'lft'=> $ this-> getLft(),
'rgt'=> $ this-> getRgt (),
'name'=> $ this-> getName(),
'md5Name'=> $ this-> getMd5Name(),
'owner'=> ; $ this-> getOwner() - > getId(),
'etag'=> $ this-> getEtag()
);
}

Hydration API文件:http://www.doctrine-project.org/api/orm/2.1/namespace-Doctrine.ORM.Internal。 Hydration.html


I have a simple entity which is a table holding my user data and I want to fetch all columns of a specific user as an array and then json_encode them but what I get is an entity object which I will have to use get method for every value. I just want an associative array of my user table values. The codes I tried and didn't work (returned entity object) are as follows: 1.

$qb = $this->em->createQueryBuilder();
$qb->add('select', 'a')
->add('from', 'Entities\Adminprofile a')
->add('where', 'a.userid = 3333');
$accounts = $qb->getQuery()->getResult();

2.

$account = $this->em->getRepository('Entities\Adminprofile')->findOneBy(
array('userid' => '3333'));

PS: im using z2d2 Project,which is doctrine2 integration into Zend framework.

解决方案

When you do $accounts = $qb->getQuery()->getResult(); the argument you pass to getResult tells it how to hydrate the result set which is will return.

Array Hydration

If you want arrays, than you should pass the CONSTANT for array hydrations Doctrine\ORM\Query::HYDRATE_ARRAY.

$accounts = $qb->getQuery()->getResult( Doctrine\ORM\Query::HYDRATE_ARRAY );

If you are using findOneBy() then it will always return an entity. Due to the internals of how find works, you cannot tell it to hydrate by any other means other than to return entities.

In this scenario, what you need to do is create a getValues() method inside of your entity which returns an array of your entity, like this:

 public function getSimpleValues(){
     return array(
        'id'      => $this->getId(),
        'lft'     => $this->getLft(),
        'rgt'     => $this->getRgt(),
        'name'    => $this->getName(),
        'md5Name' => $this->getMd5Name(),              
        'owner'   => $this->getOwner()->getId(),
        'etag'    => $this->getEtag()
    );
}

Hydration API Docs: http://www.doctrine-project.org/api/orm/2.1/namespace-Doctrine.ORM.Internal.Hydration.html

这篇关于如何获取Doctrine2结果对象作为关联数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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