php - Keep selected fields order with HYDRATE_ARRAY in Doctrine 1.2

61

My question is quite simple but I can't manage to find an answer.

When I execute a query like:

$query->select('t2.name as t2_name, t1.name as t1_name')
->from('table1 t1')
->leftJoin('t1.table2 t2')
->execute(array(), Doctrine_Core::HYDRATE_ARRAY);

Doctrine returns me an array like:

array(
[0] => array(
't1_name' => 'foo',
't2_name' => 'bar'
)
)

Where i expected to get field t2_name to be set in the array before t1_name.

Is there anyway to keep the order of these selected fields in Doctrine ?

691

Answer

Solution:

Doctrine will automatically include the primary (root) table's key field and automatically make it the first column in any query, in almost all hydration types.

Since table1 is the root table in your query, it moves that to the beginning for its own internal processing benefits.

I find this behavior annoying and somewhat unpredictable at times also, but have found great relief by creating custom hydrators.

There's a good example of creating a key/value hydrator which I have used beneficially many times in our code.

You could do something similar to rearrange the fields in the order you want.

Also I have posted an explanation to a very similar question here which may be beneficial.

People are also looking for solutions to the problem: php - Complex MySQL-query, union, intersect or natural join?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.