DQL返回实体的阵列,而不是物体 [英] DQL returning an array of entities instead of objects
本文介绍了DQL返回实体的阵列,而不是物体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
通常情况下,如果我运行一个查询DQL如低于它会返回实体对象的列表:
Normally if I run a DQL query such as below it would return a list of entity objects:
$d = $this->getDoctrine()->getRepository('xxxWebsiteBundle:Locations')->createQueryBuilder('l');
->where('l.enabled = :enabled')
->setParameter('enabled', 1)
$result= $d
->getQuery();
不过,如果我添加了一个选择,然后返回一个数组:
However, if I add a select then it returns an array:
$d = $this->getDoctrine()->getRepository('XXXWebsiteBundle:Locations')->createQueryBuilder('l');
$d
->select('l')
->addSelect(
'( 3959 * acos(cos(radians(' . $latitude . '))' .
'* cos( radians( l.latitude ) )' .
'* cos( radians( l.longitude )' .
'- radians(' . $longitude . ') )' .
'+ sin( radians(' . $latitude . ') )' .
'* sin( radians( l.latitude ) ) ) ) as distance'
)
->where('l.enabled = :enabled')
->setParameter('enabled', 1)
->having('distance < :distance')
->setParameter('distance', $requestedDistance)
->orderBy('distance', 'ASC');
$closeresult= $d
->getQuery();
因此,使用第一个查询我可以做到以下几点:
So using the first query I could do the following:
foreach($result->getResult() as $location){
echo $location->getName()
}
然而,使用我必须用我假设下第二个查询是不正确的:
However, using the second query I have to use the following which I assume isn't correct:
foreach($result->getResult() as $location){
echo $location[0]->getName()
}
任何想法我怎么能提高呢?
Any ideas how I can improve this?
推荐答案
由于学说ORM 2.2,可以使用<$c$c>HIDDEN$c$c>关键字。
Since Doctrine ORM 2.2, you can use the HIDDEN
keyword.
SELECT a, SOME_EXPR() AS HIDDEN sortCond FROM Entity a ORDER BY sortCond DESC
在你的榜样,它会像下面这样:
In your example, it would be like following:
$d = $this
->getDoctrine()
->getRepository('XXXWebsiteBundle:Locations')
->createQueryBuilder('l');
$d
->select('l')
->addSelect(
'( 3959 * acos(cos(radians(' . $latitude . '))' .
'* cos( radians( l.latitude ) )' .
'* cos( radians( l.longitude )' .
'- radians(' . $longitude . ') )' .
'+ sin( radians(' . $latitude . ') )' .
'* sin( radians( l.latitude ) ) ) ) AS HIDDEN distance'
)
->where('l.enabled = :enabled')
->setParameter('enabled', 1)
->having('distance < :distance')
->setParameter('distance', $requestedDistance)
->orderBy('distance', 'ASC');
$closeresult = $d->getQuery();
这篇关于DQL返回实体的阵列,而不是物体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文