3 namespace Rapsys\AirBundle\Repository
;
5 use Symfony\Component\Translation\TranslatorInterface
;
6 use Doctrine\ORM\Query\ResultSetMapping
;
11 class UserRepository
extends \Doctrine\ORM\EntityRepository
{
13 * Find users with translated highest group and civility
15 * @param $translator The TranslatorInterface instance
17 public function findAllWithTranslatedGroupAndCivility(TranslatorInterface
$translator) {
19 $em = $this->getEntityManager();
22 $qs = $em->getConfiguration()->getQuoteStrategy();
23 $dp = $em->getConnection()->getDatabasePlatform();
25 //Get quoted table names
26 //XXX: this allow to make this code table name independent
28 'RapsysAirBundle:UserGroup' => $qs->getJoinTableName($em->getClassMetadata('RapsysAirBundle:User')->getAssociationMapping('groups'), $em->getClassMetadata('RapsysAirBundle:User'), $dp),
29 'RapsysAirBundle:Group' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Group'), $dp),
30 'RapsysAirBundle:Civility' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Civility'), $dp),
31 'RapsysAirBundle:User' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:User'), $dp)
35 $req = 'SELECT a.id, a.forename, a.surname, a.t_id, a.t_short, a.t_title, a.g_id, a.g_title FROM (
36 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
37 FROM RapsysAirBundle:User AS u
38 JOIN RapsysAirBundle:Civility AS t ON (t.id = u.civility_id)
39 LEFT JOIN RapsysAirBundle:UserGroup AS gu ON (gu.user_id = u.id)
40 LEFT JOIN RapsysAirBundle:Group AS g ON (g.id = gu.group_id)
41 ORDER BY g.id DESC, NULL LIMIT '.PHP_INT_MAX
.'
42 ) AS a GROUP BY a.id ORDER BY NULL';
44 //Replace bundle entity name by table name
45 $req = str_replace(array_keys($tables), array_values($tables), $req);
47 //Get result set mapping instance
48 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
49 $rsm = new ResultSetMapping();
51 /*XXX: we don't want a result set for our request
52 $rsm->addEntityResult('RapsysAirBundle:User', 'u');
53 $rsm->addFieldResult('u', 'id', 'id');
54 $rsm->addFieldResult('u', 'forename', 'forename');
55 $rsm->addFieldResult('u', 'surname', 'surname');
56 $rsm->addFieldResult('u', 't_id', 'title_id');
57 $rsm->addJoinedEntityResult('RapsysAirBundle:Title', 't', 'u', 'title');
58 $rsm->addFieldResult('t', 't_id', 'id');
59 $rsm->addFieldResult('t', 't_title', 'title');
60 $rsm->addJoinedEntityResult('RapsysAirBundle:Group', 'g', 'u', 'groups');
61 $rsm->addFieldResult('g', 'g_id', 'id');
62 $rsm->addFieldResult('g', 'g_title', 'title');*/
65 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
66 //addScalarResult($sqlColName, $resColName, $type = 'string');
67 $rsm->addScalarResult('id', 'id', 'integer')
68 ->addScalarResult('forename', 'forename', 'string')
69 ->addScalarResult('surname', 'surname', 'string')
70 ->addScalarResult('t_id', 't_id', 'integer')
71 ->addScalarResult('t_short', 't_short', 'string')
72 ->addScalarResult('t_title', 't_title', 'string')
73 ->addScalarResult('g_id', 'g_id', 'integer')
74 ->addScalarResult('g_title', 'g_title', 'string')
75 ->addIndexByScalar('id');
79 ->createNativeQuery($req, $rsm)
86 foreach($res as $data) {
87 //Get translated group
88 $group = $translator->trans($data['g_title']?:'User');
89 //Get translated title
90 $title = $translator->trans($data['t_short']);
92 if (!isset($ret[$group])) {
96 //XXX: ChoiceType use display string as key
97 $ret[$group][$title.' '.$data['forename'].' '.$data['surname']] = $data['id'];
105 * Find all applicant by session
107 * @param $session The Session
109 public function findAllApplicantBySession($session) {
111 $em = $this->getEntityManager();
114 $ret = $this->getEntityManager()
115 ->createQuery('SELECT u.id, u.pseudonym FROM RapsysAirBundle:Application a JOIN RapsysAirBundle:User u WITH u.id = a.user WHERE a.session = :session')
116 ->setParameter('session', $session)
120 $ret = array_column($ret, 'id', 'pseudonym');
127 * Find all users grouped by translated group
129 * @param $translator The TranslatorInterface instance
130 * @return array|null The user array or null
132 public function findUserGroupedByTranslatedGroup(TranslatorInterface
$translator) {
134 $em = $this->getEntityManager();
137 $qs = $em->getConfiguration()->getQuoteStrategy();
138 $dp = $em->getConnection()->getDatabasePlatform();
140 //Get quoted table names
141 //XXX: this allow to make this code table name independent
143 'RapsysAirBundle:UserGroup' => $qs->getJoinTableName($em->getClassMetadata('RapsysAirBundle:User')->getAssociationMapping('groups'), $em->getClassMetadata('RapsysAirBundle:User'), $dp),
144 'RapsysAirBundle:Group' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Group'), $dp),
145 'RapsysAirBundle:User' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:User'), $dp),
146 //XXX: Set limit used to workaround mariadb subselect optimization
147 ':limit' => PHP_INT_MAX
,
154 SELECT a.id, a.pseudonym, a.g_id, a.g_title
156 SELECT u.id, u.pseudonym, g.id AS g_id, g.title AS g_title
157 FROM RapsysAirBundle:User AS u
158 JOIN RapsysAirBundle:UserGroup AS gu ON (gu.user_id = u.id)
159 JOIN RapsysAirBundle:Group AS g ON (g.id = gu.group_id)
167 //Replace bundle entity name by table name
168 $req = str_replace(array_keys($tables), array_values($tables), $req);
170 //Get result set mapping instance
171 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
172 $rsm = new ResultSetMapping();
175 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
176 //addScalarResult($sqlColName, $resColName, $type = 'string');
177 $rsm->addScalarResult('id', 'id', 'integer')
178 ->addScalarResult('pseudonym', 'pseudonym', 'string')
179 ->addScalarResult('g_id', 'g_id', 'integer')
180 ->addScalarResult('g_title', 'g_title', 'string')
181 ->addIndexByScalar('id');
185 ->createNativeQuery($req, $rsm)
192 foreach($res as $data) {
193 //Get translated group
194 $group = $translator->trans($data['g_title']?:'User');
196 //Init group subarray
197 if (!isset($ret[$group])) {
202 $ret[$group][$data['id']] = $data['pseudonym'];