]> Raphaël G. Git Repositories - airbundle/commitdiff
Add findOrganizerGroupedByGroup function
authorRaphaël Gertz <git@rapsys.eu>
Mon, 28 Dec 2020 07:50:19 +0000 (08:50 +0100)
committerRaphaël Gertz <git@rapsys.eu>
Mon, 28 Dec 2020 07:50:19 +0000 (08:50 +0100)
Repository/UserRepository.php

index 9f112ee10e322ba2656ea393ad83bd0956ba809d..8991bf4a29cea5b629d6907f62433b19a20d06c0 100644 (file)
@@ -122,4 +122,109 @@ class UserRepository extends \Doctrine\ORM\EntityRepository {
                //Send result
                return $ret;
        }
+
+       /**
+        * Find all organizer grouped by translated group
+        *
+        * @param $translator The TranslatorInterface instance
+        * @return array|null The organizer array or null
+        */
+       public function findOrganizerGroupedByGroup(TranslatorInterface $translator) {
+               //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:GroupUser' => $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
+                       ':limit' => PHP_INT_MAX,
+                       "\t" => '',
+                       "\n" => ' '
+               ];
+
+               //Set the request
+               $req = <<<SQL
+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:Group AS g ON (g.id = gu.group_id)
+       ORDER BY g.id DESC
+       LIMIT 0, :limit
+) AS a
+GROUP BY a.id
+ORDER BY a.id ASC
+SQL;
+
+               //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 all fields
+               //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
+               //addScalarResult($sqlColName, $resColName, $type = 'string');
+               $rsm->addScalarResult('id', 'id', 'integer')
+                       ->addScalarResult('pseudonym', 'pseudonym', 'string')
+                       ->addScalarResult('g_id', 'g_id', 'integer')
+                       ->addScalarResult('g_title', 'g_title', 'string')
+                       ->addIndexByScalar('id');
+
+               //Fetch result
+               $res = $em
+                       ->createNativeQuery($req, $rsm)
+                       ->getResult();
+
+               //Init return
+               $ret = [];
+
+               //Process result
+               foreach($res as $data) {
+                       //Get translated group
+                       $group = $translator->trans($data['g_title']?:'User');
+
+                       //Init group subarray
+                       if (!isset($ret[$group])) {
+                               $ret[$group] = [];
+                       }
+
+                       //Set data
+                       $ret[$group][$data['id']] = $data['pseudonym'];
+               }
+
+               //Send result
+               return $ret;
+
+               header('Content-Type: text/plain');
+               var_dump($ret);
+               exit;
+
+               //Get entity manager
+               $em = $this->getEntityManager();
+
+               //Fetch sessions
+               $ret = $this->getEntityManager()
+                       ->createQuery('SELECT u.id, u.pseudonym, g.title FROM RapsysAirBundle:User u JOIN RapsysAirBundle:GroupUser gu WITH gu.user = u.id JOIN RapsysAirBundle:Group g WITH g.id = gu.group GROUP BY u.id ORDER BY g.id DESC')
+                       ->getResult();
+
+               header('Content-Type: text/plain');
+               var_dump($ret);
+               exit;
+
+               //Process result
+               $ret = array_column($ret, 'id', 'pseudonym');
+
+               //Send result
+               return $ret;
+       }
 }