19

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.

MLavoie
  • 9,671
  • 41
  • 36
  • 56
Mehdi Fanai
  • 4,021
  • 13
  • 50
  • 75

4 Answers4

37

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

Wilt
  • 41,477
  • 12
  • 152
  • 203
Layke
  • 51,422
  • 11
  • 85
  • 111
  • 3
    Thanks for fast and precise reply.The constants for the different hydration modes are: Query::HYDRATE_OBJECT Query::HYDRATE_ARRAY Query::HYDRATE_SCALAR Query::HYDRATE_SINGLE_SCALAR – Mehdi Fanai Aug 31 '11 at 16:01
  • what if i want to use like `$this->doctrine->em->find('Entity\User',5)`? – Wit Wikky Aug 07 '14 at 09:54
  • 1
    This my problem , http://stackoverflow.com/questions/25158549/doctrine-entity-object-to-array – Wit Wikky Aug 07 '14 at 09:55
26

You can also use getArrayResult() as a shortcut to passing in the constant to get an array back:

$accounts = $qb->getQuery()->getArrayResult();
Jeremy Hicks
  • 3,690
  • 5
  • 40
  • 52
7

You should use constant containing value 2 and it is inbuilt, you can do it like this at the end part of your query

$qb->getQuery()->getResult( Doctrine\ORM\Query::HYDRATE_ARRAY );
Jaskaran Singh
  • 2,392
  • 24
  • 39
2
$data = $this->entity->findOneBy(array('key' => $value));

$hydrator = new \DoctrineModule\Stdlib\Hydrator\DoctrineObject($this->_em, $entity_name);

$array = $hydrator->extract($data);
Cleb
  • 25,102
  • 20
  • 116
  • 151
  • This is not an answer to the question. – Mogsdad Nov 11 '15 at 21:47
  • Since this is the only correct answer when using laminas (former zend framework), this should be updated for the year 2021. The laminas-hydrator component (module) brings classes, that easily can hydrate data into an object or extract data from an initialized object. Doctrine brings it 's own hydrator called `DoctrineObject`, which inherits from Laminas `AbstractHydrator` class. Executing the `DoctrineObject::extract()` method with the data object as parameter leads to the expected result: An assoc array with the data from the object. – Marcel Aug 12 '21 at 06:35
  • The call in 2021: `(new DoctrineObject($entityManager))->extract($entity)`; – Marcel Aug 12 '21 at 06:37