I'm learning Symfony2 and I don't understand because a DQL query not works but SQL query works correctly.
My DB is this:
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 that works correctly:
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)
My DQL query in EventRepository that not works:
$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)
The runnable DQL query detailed in Symfony's profiler:
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;
This is my indexAction in my EventController:
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,
));
}
And in my EventRepository I define the method 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();
}
I don't understand why not works. The query returns 0 rows and it must return 3 rows :( Please help.