DQL查询不起作用,但SQL查询有效

时间:2016-11-19 17:30:07

标签: mysql sql symfony doctrine-orm dql

我正在学习Symfony2,我不明白,因为DQL查询不起作用,但SQL查询工作正常。

我的数据库是这样的:

mysql> 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,00 sec)

正常运行的SQL:

mysql> select * from event where lat > 40.34 and lat < 40.37 and lng > -0.07 and lng < -0.05 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)

在EventRepository中我的DQL查询不起作用:

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

            // WHERE (e.lat BETWEEN :latBoundMin AND :latBoundMax) AND (e.lng BETWEEN :lngBoundMin AND :lngBoundMax)

Symfony的分析器中详述的可运行DQL查询:

SELECT e0_.id AS id_0, e0_.title AS title_1, e0_.location AS location_2, e0_.lat AS lat_3, e0_.lng AS lng_4, e0_.date AS date_5, e0_.program AS program_6, e0_.createdAt AS createdAt_7, e0_.updatedAt AS updatedAt_8, e0_.promoter_id AS promoter_id_9 FROM event e0_ WHERE e0_.lat >= 40.3457789 AND e0_.lat <= 40.3707789 AND e0_.lng >= -0.07870989999999 AND e0_.lng <= -0.05370989999999 ORDER BY e0_.date ASC;

这是我在EventController中的indexAction:

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

            $events = $em->getRepository("AppBundle:Event")->findByBounds($lat, $lng, $zoom);

        return $this->render('event/index.html.twig', array(
            'events' => $events,
        ));
    }

在我的EventRepository中,我定义了方法findByBounds:

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 >= :latBoundMin AND e.lat <= :latBoundMax AND e.lng >= :lngBoundMin AND e.lng <= :lngBoundMax
        ORDER BY e.date ASC"
    );
    // WHERE (e.lat BETWEEN :latBoundMin AND :latBoundMax) AND (e.lng BETWEEN :lngBoundMin AND :lngBoundMax)

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

    return $query->getResult();
}

我不明白为什么不行。查询返回0行,它必须返回3行:(请帮助。

0 个答案:

没有答案