0
我正在開發一個小網站來學習Symfony2,並且我對Doctrine查詢有問題,但我不明白爲什麼。查詢與Doctrine2無法正常工作
這是我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
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,
));
}
在數據庫中的所有事件的indexAction ...
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查詢不正常工作:(
入住的Symfony的探查,什麼確切的SQL查詢正在運行並根據您的期望進行驗證。另外,我會在進行多次計算時添加合適的大括號,以便確定運算符的優先順序。我的意思是在您的存儲庫方法中計算值。 –
感謝您的信息:) –