在 Doctrine 1.2 中使用 HYDRATE_ARRAY 保持选定字段的顺序
我的问题很简单,但我无法找到答案。
当我执行如下查询时:
$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 返回一个数组,如:
array(
[0] => array(
't1_name' => 'foo',
't2_name' => 'bar'
)
)
我希望在数组中的 t1_name 之前设置字段 t2_name 。
是否有办法保持 Doctrine 中这些选定字段的顺序?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Doctrine 将自动包含主(根)表的关键字段,并自动使其成为任何查询(几乎所有水合类型)中的第一列。
由于 table1 是查询中的根表,因此它会将其移动到开头以获得其自身的内部处理优势。
我发现这种行为很烦人,有时也有些不可预测,但通过创建定制水化器,我发现了很大的缓解。
有一个 创建的好例子一个键/值水合器,我在我们的代码中多次使用过它,效果非常好。
您可以执行类似的操作,按照您想要的顺序重新排列字段。
我还发布了对一个非常类似的问题的解释 这可能是有益的。
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.