From: Raphaƫl Gertz Date: Fri, 21 May 2021 00:38:10 +0000 (+0200) Subject: Cleanup X-Git-Tag: 0.2.3~4 X-Git-Url: https://git.rapsys.eu/.gitweb.cgi/airbundle/commitdiff_plain/401c43f206e2952e9608ce83c2b8e897b438de6f?hp=c21ece30ad66b4ce32444c7c913bf65883000b66 Cleanup Add location findByUserId method Rename GroupUser alias to UserGroup --- diff --git a/Repository/LocationRepository.php b/Repository/LocationRepository.php index a7b351a..129a176 100644 --- a/Repository/LocationRepository.php +++ b/Repository/LocationRepository.php @@ -3,6 +3,7 @@ namespace Rapsys\AirBundle\Repository; use Symfony\Component\Translation\TranslatorInterface; +use Doctrine\ORM\Query\ResultSetMapping; /** * LocationRepository @@ -107,8 +108,7 @@ ORDER BY '.(!empty($userId)?'COUNT(a.id) DESC, ':'').'COUNT(s.id) DESC, l.id' public function fetchTranslatedUserLocationByDatePeriod(TranslatorInterface $translator, $period, $userId) { //Fetch sessions $ret = $this->getEntityManager() - ->createQuery('SELECT l.id, l.title FROM RapsysAirBundle:Application a JOIN RapsysAirBundle:Session s JOIN RapsysAirBundle:Location l WHERE a.user = :uid AND a.session = s.id AND s.date BETWEEN :begin AND :end AND s.location = l.id GROUP BY l.id ORDER BY l.id') -#SELECT l.id, l.title FROM RapsysAirBundle:Session s JOIN RapsysAirBundle:Application a JOIN RapsysAirBundle:Location l WHERE s.date BETWEEN :begin AND :end AND s.id = a.session AND l.id = s.location GROUP BY l.id ORDER BY l.id + ->createQuery('SELECT l.id, l.title FROM RapsysAirBundle:Application a JOIN RapsysAirBundle:Session s JOIN RapsysAirBundle:Location l WHERE a.user = :uid AND a.session = s.id AND s.date BETWEEN :begin AND :end AND s.location = l.id GROUP BY l.id ORDER BY l.id') ->setParameter('begin', $period->getStartDate()) ->setParameter('end', $period->getEndDate()) ->setParameter('uid', $userId) @@ -125,4 +125,50 @@ ORDER BY '.(!empty($userId)?'COUNT(a.id) DESC, ':'').'COUNT(s.id) DESC, l.id' //Send result return $ret; } + + /** + * Find locations by user id + * + * @param $id The user id + * @return array The user locations + */ + public function findByUserId($userId) { + //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:UserLocation' => $qs->getJoinTableName($em->getClassMetadata('RapsysAirBundle:User')->getAssociationMapping('locations'), $em->getClassMetadata('RapsysAirBundle:User'), $dp), + 'RapsysAirBundle:Location' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Location'), $dp) + ]; + + //Set the request + $req = 'SELECT l.id, l.title +FROM RapsysAirBundle:UserLocation AS ul +JOIN RapsysAirBundle:Location AS l ON (l.id = ul.location_id) +WHERE ul.user_id = :uid'; + + //Replace bundle entity name by table name + $req = str_replace(array_keys($tables), array_values($tables), $req); + + //Get result set mapping instance + //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php + $rsm = new ResultSetMapping(); + + //Declare result set for our request + $rsm->addEntityResult('RapsysAirBundle:Location', 'l'); + $rsm->addFieldResult('l', 'id', 'id'); + $rsm->addFieldResult('l', 'title', 'title'); + + //Send result + return $em + ->createNativeQuery($req, $rsm) + ->setParameter('uid', $userId) + ->getResult(); + } } diff --git a/Repository/UserRepository.php b/Repository/UserRepository.php index 8660e39..e7ff076 100644 --- a/Repository/UserRepository.php +++ b/Repository/UserRepository.php @@ -25,7 +25,7 @@ class UserRepository extends \Doctrine\ORM\EntityRepository { //Get quoted table names //XXX: this allow to make this code table name independent $tables = [ - 'RapsysAirBundle:GroupUser' => $qs->getJoinTableName($em->getClassMetadata('RapsysAirBundle:User')->getAssociationMapping('groups'), $em->getClassMetadata('RapsysAirBundle:User'), $dp), + 'RapsysAirBundle:UserGroup' => $qs->getJoinTableName($em->getClassMetadata('RapsysAirBundle:User')->getAssociationMapping('groups'), $em->getClassMetadata('RapsysAirBundle:User'), $dp), 'RapsysAirBundle:Group' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Group'), $dp), 'RapsysAirBundle:Civility' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Civility'), $dp), 'RapsysAirBundle:User' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:User'), $dp) @@ -36,7 +36,7 @@ class UserRepository extends \Doctrine\ORM\EntityRepository { SELECT u.id, u.forename, u.surname, t.id AS t_id, t.short AS t_short, t.title AS t_title, g.id AS g_id, g.title AS g_title FROM RapsysAirBundle:User AS u JOIN RapsysAirBundle:Civility AS t ON (t.id = u.civility_id) - LEFT JOIN RapsysAirBundle:GroupUser AS gu ON (gu.user_id = u.id) + LEFT JOIN RapsysAirBundle:UserGroup AS gu ON (gu.user_id = u.id) LEFT JOIN RapsysAirBundle:Group AS g ON (g.id = gu.group_id) ORDER BY g.id DESC, NULL LIMIT '.PHP_INT_MAX.' ) AS a GROUP BY a.id ORDER BY NULL'; @@ -140,7 +140,7 @@ class UserRepository extends \Doctrine\ORM\EntityRepository { //Get quoted table names //XXX: this allow to make this code table name independent $tables = [ - 'RapsysAirBundle:GroupUser' => $qs->getJoinTableName($em->getClassMetadata('RapsysAirBundle:User')->getAssociationMapping('groups'), $em->getClassMetadata('RapsysAirBundle:User'), $dp), + 'RapsysAirBundle:UserGroup' => $qs->getJoinTableName($em->getClassMetadata('RapsysAirBundle:User')->getAssociationMapping('groups'), $em->getClassMetadata('RapsysAirBundle:User'), $dp), 'RapsysAirBundle:Group' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Group'), $dp), 'RapsysAirBundle:User' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:User'), $dp), //XXX: Set limit used to workaround mariadb subselect optimization @@ -155,7 +155,7 @@ SELECT a.id, a.pseudonym, a.g_id, a.g_title FROM ( SELECT u.id, u.pseudonym, g.id AS g_id, g.title AS g_title FROM RapsysAirBundle:User AS u - JOIN RapsysAirBundle:GroupUser AS gu ON (gu.user_id = u.id) + JOIN RapsysAirBundle:UserGroup AS gu ON (gu.user_id = u.id) JOIN RapsysAirBundle:Group AS g ON (g.id = gu.group_id) ORDER BY g.id DESC LIMIT 0, :limit