2012-11-22 70 views
1

我已经得到了以下doctrine2查询很好地工作,它检索某些地理半径内的所有“标记”。如何做Doctrine2计算字段排序

$qb->select('marker') 
     ->from('SndSpecialistLocator\Entity\Marker', 'marker') 
     ->where('DISTANCE(marker.location, POINT_STR(:point)) < :distance') 
     ->setParameter('point', $point) 
     ->setParameter('distance', $radius); 

现在我想按距离排序它们。

$qb->select('marker (DISTANCE(marker.location, POINT_STR(:point))) AS distance') 
     ->from('SndSpecialistLocator\Entity\Marker', 'marker') 
     ->where('DISTANCE(marker.location, POINT_STR(:point)) < :distance') 
     ->setParameter('point', $point) 
     ->orderBy('distance', 'DESC') 
     ->setParameter('distance', $radius); 

但不幸的是,这并不工作,我想知道这是可能的距离不是我的实体的不动产,但计算的呢?

这里有什么窍门?

回答

0

不幸的是,按别名排序是不可能的。 你可以做什么*反而是手动重复功能,在您的排序依据声明:

$qb->select('marker (DISTANCE(marker.location, POINT_STR(:point))) AS distance') 
    ->from('SndSpecialistLocator\Entity\Marker', 'marker') 
    ->where('DISTANCE(marker.location, POINT_STR(:point)) < :distance') 
    ->setParameter('point', $point) 
    ->orderBy('DISTANCE(marker.location, POINT_STR(:point))', 'DESC') 
    ->setParameter('distance', $radius); 

* 我们可能都将结束在DEV-地狱走下干

0

圣路找到解决方案。

$qb->select('m as marker, (DISTANCE(m.location, POINT_STR(:point))) as distance') 
     ->from('SndSpecialistLocator\Entity\Marker', 'm') 
     ->where('DISTANCE(m.location, POINT_STR(:point)) < :distance') 
     ->setParameter('point', $point) 
     ->orderBy('distance', 'ASC') 
     ->setParameter('distance', $radius) 
     ->setMaxResults(5); 

    $query = $qb->getQuery(); 
    $result = $query->execute(); 

    /** 
    * Convert the resulting associative array into one containing only the entity objects 
    * 
    * array (size=5) 
    * 0 => 
    *  array (size=2) 
    *  'marker' => 
    *   object(SndSpecialistLocator\Entity\Marker)[647] 
    *   protected 'id' => int 43 
    *   protected 'title' => string 'c5d07acdd47efbe38a6d0bf4ec64f333' (length=32) 
    *   private 'location' => 
    *    object(SndSpecialistLocator\Orm\Point)[636] 
    *    private 'lat' => float 52.2897 
    *    private 'lng' => float 4.84268 
    *  'distance' => string '0.0760756823433058' (length=18) 
    * 1 => ... 
    * 
    * array (size=5) 
    * 0 => 
    *  object(SndSpecialistLocator\Entity\Marker)[647] 
    *  protected 'id' => int 43 
    *  protected 'title' => string 'c5d07acdd47efbe38a6d0bf4ec64f333' (length=32) 
    *  private 'location' => 
    *   object(SndSpecialistLocator\Orm\Point)[636] 
    *   private 'lat' => float 52.2897 
    *   private 'lng' => float 4.84268 
    * 1 => ... 
    * 
    */ 
    array_walk($result, function (&$item, $key) 
    { 
     $item = $item['marker']; 
    }); 
2

或者,尝试在呼叫使用隐藏选择():

$qb->select('m as marker, (DISTANCE(m.location, POINT_STR(:point))) as HIDDEN distance') 
// note HIDDEN added here --->------->------->------->------->------->---^ 
    ->from('SndSpecialistLocator\Entity\Marker', 'm') 
    ->where('DISTANCE(m.location, POINT_STR(:point)) < :distance') 
    ->setParameter('point', $point) 
    ->orderBy('distance', 'ASC') 
    ->setParameter('distance', $radius) 
    ->setMaxResults(5); 

$query = $qb->getQuery(); 
$result = $query->execute(); 

添加隐藏到SELECT子句从结果中隐藏,但允许它的排序依据子句中使用。然后你的$结果应该包含你想要的对象,而不必做额外的array_walk。

+0

优秀的答案,这正是我一直在寻找的。拿我的upvote – CharlyDelta