Usually, if I run a DQL query, for example, below, it returns 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 select, 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 should use the following, which I assume is incorrect:
foreach($result->getResult() as $location){
echo $location[0]->getName()
}
Any ideas how I can improve this?
source
share