查询无法与Doctrine2正常工作

时间:2016-11-18 15:42:35

标签: jquery symfony doctrine-orm dql

我正在开发一个小网站来学习Symfony2,我对Doctrine查询有疑问,我不明白为什么。

这是我的EventRepository

class EventRepository extends \Doctrine\ORM\EntityRepository
{
    public function findByBounds($lat, $lng, $zoom)
    {
        $em = $this->getEntityManager();
        $bounds = 0.025;

        $latBound1 = $lat + $bounds * 2 ** $zoom / 2;
        $latBound2 = $lat - $bounds * 2 ** $zoom / 2;
        $lngBound1 = $lng + $bounds * 2 ** $zoom / 2;
        $lngBound2 = $lng - $bounds * 2 ** $zoom / 2;

        $latBoundMin = $latBound1 < $latBound2 ? $latBound1 : $latBound2;
        $latBoundMax = $latBound1 > $latBound2 ? $latBound1 : $latBound2;
        $lngBoundMin = $lngBound1 < $lngBound2 ? $lngBound1 : $lngBound2;
        $lngBoundMax = $lngBound1 > $lngBound2 ? $lngBound1 : $lngBound2;

        $query = $em->createQuery(
            "SELECT e
            FROM AppBundle:Event e
            WHERE (e.lat BETWEEN :latBoundMin AND :latBoundMax) AND (e.lng BETWEEN :lngBoundMin AND :lngBoundMax)
            ORDER BY e.date ASC");

        $query->setParameters(array(
            "latBoundMin" => $latBoundMin,
            "latBoundMax" => $latBoundMax,
            "lngBoundMin" => $lngBoundMin,
            "lngBoundMax" => $lngBoundMax,
        ));

        return $query->getResult();
    }
}

这是我在EventController中的indexAction

public function indexAction(Request $request)
{
    $em = $this->getDoctrine()->getManager();
    $lat = $request->request->get("locationLat");
    $lng = $request->request->get("locationLng");
    $zoom = 0;

    // search events by lat, lng and bounds
    // $events = $em->getRepository('AppBundle:Event')->findAll();
    // do {
    //
        $latBound1 = $lat + 0.025 * 2 ** $zoom / 2;
        $latBound2 = $lat - 0.025 * 2 ** $zoom / 2;
        $lngBound1 = $lng + 0.025 * 2 ** $zoom / 2;
        $lngBound2 = $lng - 0.025 * 2 ** $zoom / 2;

        $latBoundMin = $latBound1 < $latBound2 ? $latBound1 : $latBound2;
        $latBoundMax = $latBound1 > $latBound2 ? $latBound1 : $latBound2;
        $lngBoundMin = $lngBound1 < $lngBound2 ? $lngBound1 : $lngBound2;
        $lngBoundMax = $lngBound1 > $lngBound2 ? $lngBound1 : $lngBound2;
    //
    //     $events = $em->getRepository("AppBundle:Event")->findByBounds($lat, $lng, $zoom);
    //     $numEvents = 0;
    //     foreach ($events as $event) {
    //         $numEvents++;
    //     }
    // } while ($numEvents < 5 && $zoom <= 3);

    $events = $em->getRepository("AppBundle:Event")->findByBounds($lat, $lng, $zoom);
    $numEvents = 0;
    foreach ($events as $event) {
        $numEvents++;
    }

    return $this->render('event/index.html.twig', array(
        'events' => $events,
        "lat" => $lat,
        "lng" => $lng,
        "zoom" => $zoom,
        "numEvents" => $numEvents,
        "lat1" => $latBoundMin,
        "lat2" => $latBoundMax,
        "lng1" => $lngBoundMin,
        "lng2" => $lngBoundMax,
    ));
}

DB中的所有事件......

mysql> select * from event;
+----+-------------+------------------+-------------------------+-------------------+----------------------+---------------------+---------+---------------------+---------------------+
| id | promoter_id | title            | location                | lat               | lng                  | date                | program | createdAt           | updatedAt           |
+----+-------------+------------------+-------------------------+-------------------+----------------------+---------------------+---------+---------------------+---------------------+
|  1 |           2 | St Blai VdC 2017 | Vilar de Canes, Espanya | 40.35827889999999 | -0.06620989999998983 | 2017-02-04 00:00:00 | :)      | 2016-11-16 13:52:58 | 2016-11-16 13:52:58 |
|  2 |           1 | Festes Agost VdC | Vilar de Canes, Espanya | 40.35827889999999 | -0.06620989999998983 | 2017-08-04 00:00:00 | xD      | 2016-11-16 13:53:39 | 2016-11-16 13:53:39 |
|  3 |           6 | Festes Agost VdC | Vilar de Canes, Espanya | 40.35827889999999 | -0.06620989999998983 | 2017-08-05 00:00:00 | xxxD    | 2016-11-16 13:54:24 | 2016-11-16 13:54:24 |
|  4 |           1 | qwer             | Castelló, Espanya       | 40.14517720000001 | -0.14949879999994664 | 2016-11-17 15:50:00 | asdfas  | 2016-11-17 15:51:27 | 2016-11-17 15:51:27 |
|  5 |           1 | lkn              | Castelló, Espanya       | 40.14517720000001 | -0.14949879999994664 | 2016-11-17 22:43:00 | asdf    | 2016-11-17 22:43:31 | 2016-11-17 22:43:31 |
|  6 |           1 | lkñk             | Albocàsser, Espanya     | 40.35670100000001 | 0.025070499999969797 | 2016-11-17 22:43:00 | lkjo    | 2016-11-17 22:43:58 | 2016-11-17 22:43:58 |
|  7 |           2 | ertop            | Benassal, Espanya       | 40.3797068        | -0.14195100000006278 | 2016-12-17 22:44:00 | lñkjok  | 2016-11-17 22:44:27 | 2016-11-17 22:44:27 |
+----+-------------+------------------+-------------------------+-------------------+----------------------+---------------------+---------+---------------------+---------------------+
7 rows in set (0,01 sec)

SQL中的查询有效......

mysql> select * from event where lat between 40.3082 and 40.4082 and lng between -0.1162 and -0.0162 order by date asc;
+----+-------------+------------------+-------------------------+-------------------+----------------------+---------------------+---------+---------------------+---------------------+
| id | promoter_id | title            | location                | lat               | lng                  | date                | program | createdAt           | updatedAt           |
+----+-------------+------------------+-------------------------+-------------------+----------------------+---------------------+---------+---------------------+---------------------+
|  1 |           2 | St Blai VdC 2017 | Vilar de Canes, Espanya | 40.35827889999999 | -0.06620989999998983 | 2017-02-04 00:00:00 | :)      | 2016-11-16 13:52:58 | 2016-11-16 13:52:58 |
|  2 |           1 | Festes Agost VdC | Vilar de Canes, Espanya | 40.35827889999999 | -0.06620989999998983 | 2017-08-04 00:00:00 | xD      | 2016-11-16 13:53:39 | 2016-11-16 13:53:39 |
|  3 |           6 | Festes Agost VdC | Vilar de Canes, Espanya | 40.35827889999999 | -0.06620989999998983 | 2017-08-05 00:00:00 | xxxD    | 2016-11-16 13:54:24 | 2016-11-16 13:54:24 |
+----+-------------+------------------+-------------------------+-------------------+----------------------+---------------------+---------+---------------------+---------------------+
3 rows in set (0,00 sec)

我无法理解为什么我在DQL中的查询无法正常工作:(

1 个答案:

答案 0 :(得分:0)

更改

$query->setParameters(array(
            "latBoundMin" => $latBoundMin,
            "latBoundMax" => $latBoundMax,
            "lngBoundMin" => $lngBoundMin,
            "lngBoundMax" => $lngBoundMax,
        ));

$query->setParameter('latBoundMin', $latBoundMin)
      ->setParameter('latBoundMax', $latBoundMax)
      ->setParameter('lngBoundMin', $lngBoundMin)
      ->setParameter('lngBoundMax', $lngBoundMax);