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.

1

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.