如何从DQL查询返回对象? [英] How to return object from a DQL query?
问题描述
$ qb-> select('r.position')
- > from('\Entities\Races','r')
- > where($ qb-> expr() - > eq('r.entrantId' ':entrant_id'))
- > setParameter('entrant_id',$ this-> entrantId);
$ query = $ qb-> getQuery();
$ aRaces = $ query-> getResult();
目前,它在数组中返回查询结果,如下所示:
数组
(
[0] =>数组
(
[position] => 10
)
[1] =>数组
(
[position] => 4
)
)
我希望结果返回一个Races 对象数组,以便我可以访问方法与对象关联(我很确定默认情况下以前版本的Doctrine返回对象)。
我试过:
$ aRaces = $ query-> getResult(Query :: HYDRATE_OBJECT);
但这没有什么不同。
欣赏帮助
您只能获取位置
从DB。尝试用 select(r)
替换 select('r.position')
。请参阅 DQL参考
如果您只需要 position
属性的对象,请参阅部分对象
I have written a DQL query in Doctrine 2:
$qb->select('r.position')
->from('\Entities\Races', 'r')
->where($qb->expr()->eq('r.entrantId', ':entrant_id'))
->setParameter('entrant_id', $this->entrantId);
$query = $qb->getQuery();
$aRaces = $query->getResult();
Currently it returns the results of the query in an array like so:
Array
(
[0] => Array
(
[position] => 10
)
[1] => Array
(
[position] => 4
)
)
I want the result to return an array of Races objects so that I can access the methods associated with the object (I'm pretty sure the previous version of Doctrine returned objects by default).
I tried:
$aRaces = $query->getResult(Query::HYDRATE_OBJECT);
But that didn't make a difference.
Appreciate the help
You are fetching only position
column from DB. Try replacing select('r.position')
with select(r)
. See DQL reference
If you need objects with only position
attribute, refer to partial objects
这篇关于如何从DQL查询返回对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!