+ /**
+ * Find one session by location and user id within last month
+ *
+ * @param $location The location id
+ * @param $user The user id
+ */
+ public function findOneWithinLastMonthByLocationUser($location, $user) {
+ //Get entity manager
+ $em = $this->getEntityManager();
+
+ //Get quote strategy
+ $qs = $em->getConfiguration()->getQuoteStrategy();
+ $dp = $em->getConnection()->getDatabasePlatform();
+
+ //Get quoted table names
+ //XXX: this allow to make this code table name independent
+ $tables = [
+ 'RapsysAirBundle:Session' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Session'), $dp),
+ 'RapsysAirBundle:Application' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Application'), $dp),
+ "\t" => '',
+ "\n" => ' '
+ ];
+
+ //Set the request
+ //XXX: give the gooddelay to guest just in case
+ $req =<<<SQL
+SELECT s.id
+FROM RapsysAirBundle:Session s
+JOIN RapsysAirBundle:Application a ON (a.id = s.application_id AND a.user_id = :uid AND (a.canceled IS NULL OR TIMESTAMPDIFF(DAY, a.canceled, ADDTIME(s.date, s.begin)) < 1))
+WHERE s.location_id = :lid AND s.date >= DATE_ADD(DATE_SUB(NOW(), INTERVAL 1 MONTH), INTERVAL :gooddelay DAY)
+SQL;
+
+ //Replace bundle entity name by table name
+ $req = str_replace(array_keys($tables), array_values($tables), $req);
+
+ //Get result set mapping instance
+ $rsm = new ResultSetMapping();
+
+ //Declare all fields
+ //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
+ $rsm->addScalarResult('id', 'id', 'integer')
+ ->addIndexByScalar('id');
+
+ //Return result
+ return $em
+ ->createNativeQuery($req, $rsm)
+ ->setParameter('lid', $location)
+ ->setParameter('uid', $user)
+ ->setParameter('gooddelay', self::GOOD_DELAY)
+ ->getOneOrNullResult();
+ }
+