如何进行原则2计算字段排序



我有以下 doctrine2 查询工作得很好,它检索了某个地理半径内的所有'标记'。

    $qb->select('marker')
        ->from('SndSpecialistLocatorEntityMarker', '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('SndSpecialistLocatorEntityMarker', 'marker')
        ->where('DISTANCE(marker.location, POINT_STR(:point)) < :distance')
        ->setParameter('point', $point)
        ->orderBy('distance', 'DESC')
        ->setParameter('distance', $radius);

但不幸的是,这不起作用,我想知道这是否可能,因为距离不是我实体的真正属性,而是一个计算的属性?

这有什么诀窍?

或者,尝试在调用中使用 HIDDEN 来 select() :

$qb->select('m as marker, (DISTANCE(m.location, POINT_STR(:point))) as HIDDEN distance')
// note HIDDEN added here --->------->------->------->------->------->---^
    ->from('SndSpecialistLocatorEntityMarker', '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();

将 HIDDEN 添加到 SELECT 子句会将其从结果中隐藏,但允许在 orderby 子句中使用它。然后,您的$result应包含所需的对象,而无需执行额外的array_walk。

不幸的是,无法按别名排序。相反,您可以做的*是在 orderBy 语句中手动重复该函数:

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

*我们可能最终都会因为踏上 DRY 的圣道而陷入开发地狱

找到了解决方案。

   $qb->select('m as marker, (DISTANCE(m.location, POINT_STR(:point))) as distance')
        ->from('SndSpecialistLocatorEntityMarker', '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(SndSpecialistLocatorEntityMarker)[647]
     *           protected 'id' => int 43
     *           protected 'title' => string 'c5d07acdd47efbe38a6d0bf4ec64f333' (length=32)
     *           private 'location' =>
     *             object(SndSpecialistLocatorOrmPoint)[636]
     *                private 'lat' => float 52.2897
     *                private 'lng' => float 4.84268
     *       'distance' => string '0.0760756823433058' (length=18)
     *   1 => ...
     *
     * array (size=5)
     *   0 =>
     *     object(SndSpecialistLocatorEntityMarker)[647]
     *       protected 'id' => int 43
     *       protected 'title' => string 'c5d07acdd47efbe38a6d0bf4ec64f333' (length=32)
     *       private 'location' =>
     *         object(SndSpecialistLocatorOrmPoint)[636]
     *           private 'lat' => float 52.2897
     *           private 'lng' => float 4.84268
     *   1 => ...
     *
     */
    array_walk($result, function (&$item, $key)
    {
        $item = $item['marker'];
    });

最新更新