0

I have an object which I want to convert into an array. What I really need is array of data for current record from DB. Serializer can't help because of recursive structure of the object.

Something like Doctrine::HYDRATE_ARRAY but for already fetched object.

Supervision
  • 1,683
  • 1
  • 18
  • 23
  • 1
    Possible duplicate of [Convert Entity to array in Symfony](http://stackoverflow.com/questions/21935593/convert-entity-to-array-in-symfony) – Egg Mar 18 '16 at 14:09

1 Answers1

1

You could do it this way, without the need to convert data. In the query, do not select all the object of the entity but only fields you want. This way the query returns an array of arrays, like:

$results  = array(
    0 => array('col1' => 'some_data', 'col2' => 'some_other_data'),
    1 => array('col1' => 'some_data', 'col2' => 'some_other_data')
);

and not like:

$results  = array(
    0 => Object of type Categories(for example),
    1 => Object of type Categories(for example)
);

The query should be like this:

$em->createQueryBuilder('c')
   ->select('c.name, c.shortName, c.some_column')
   ->from('AppBundle:Entity:Category', 'c')
   ->getQuery()
   ->getResult();
angrinessfap
  • 464
  • 1
  • 4
  • 10