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:User' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:User'), $dp)
34 $req = 'SELECT a.id, a.pseudonym, a.g_id, a.g_title FROM (
35 SELECT u.id, u.pseudonym, g.id AS g_id, g.title AS g_title
36 FROM RapsysAirBundle:User AS u
37 LEFT JOIN RapsysAirBundle:UserGroup AS gu ON (gu.user_id = u.id)
38 LEFT JOIN RapsysAirBundle:Group AS g ON (g.id = gu.group_id)
39 ORDER BY g.id DESC, NULL LIMIT '.PHP_INT_MAX
.'
40 ) AS a GROUP BY a.id ORDER BY NULL';
42 //Replace bundle entity name by table name
43 $req = str_replace(array_keys($tables), array_values($tables), $req);
45 //Get result set mapping instance
46 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
47 $rsm = new ResultSetMapping();
50 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
51 //XXX: we don't use a result set as we want to translate group and civility
52 $rsm->addScalarResult('id', 'id', 'integer')
53 ->addScalarResult('pseudonym', 'pseudonym', 'string')
54 ->addScalarResult('g_id', 'g_id', 'integer')
55 ->addScalarResult('g_title', 'g_title', 'string')
56 ->addIndexByScalar('id');
60 ->createNativeQuery($req, $rsm)
67 foreach($res as $data) {
68 //Without group or simple user
69 if (empty($data['g_title']) || $data['g_title'] == 'User') {
73 //Get translated group
74 $group = $translator->trans($data['g_title']);
76 if (!isset($ret[$group])) {
80 //XXX: ChoiceType use display string as key
81 $ret[$group][$data['pseudonym']] = $data['id'];
89 * Find all applicant by session
91 * @param $session The Session
93 public function findAllApplicantBySession($session) {
95 $em = $this->getEntityManager();
98 $ret = $this->getEntityManager()
99 ->createQuery('SELECT u.id, u.pseudonym FROM RapsysAirBundle:Application a JOIN RapsysAirBundle:User u WITH u.id = a.user WHERE a.session = :session')
100 ->setParameter('session', $session)
104 $ret = array_column($ret, 'id', 'pseudonym');
111 * Find all users grouped by translated group
113 * @param $translator The TranslatorInterface instance
114 * @return array|null The user array or null
116 public function findUserGroupedByTranslatedGroup(TranslatorInterface
$translator) {
118 $em = $this->getEntityManager();
121 $qs = $em->getConfiguration()->getQuoteStrategy();
122 $dp = $em->getConnection()->getDatabasePlatform();
124 //Get quoted table names
125 //XXX: this allow to make this code table name independent
127 'RapsysAirBundle:UserGroup' => $qs->getJoinTableName($em->getClassMetadata('RapsysAirBundle:User')->getAssociationMapping('groups'), $em->getClassMetadata('RapsysAirBundle:User'), $dp),
128 'RapsysAirBundle:Group' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Group'), $dp),
129 'RapsysAirBundle:User' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:User'), $dp),
130 //XXX: Set limit used to workaround mariadb subselect optimization
131 ':limit' => PHP_INT_MAX
,
138 SELECT u.id, u.mail, u.pseudonym, g.id AS g_id, g.title AS g_title
139 FROM RapsysAirBundle:User AS u
140 JOIN RapsysAirBundle:UserGroup AS gu ON (gu.user_id = u.id)
141 JOIN RapsysAirBundle:Group AS g ON (g.id = gu.group_id)
142 ORDER BY g.id DESC, u.id ASC
145 //Replace bundle entity name by table name
146 $req = str_replace(array_keys($tables), array_values($tables), $req);
148 //Get result set mapping instance
149 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
150 $rsm = new ResultSetMapping();
153 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
154 //addScalarResult($sqlColName, $resColName, $type = 'string');
155 $rsm->addScalarResult('id', 'id', 'integer')
156 ->addScalarResult('mail', 'mail', 'string')
157 ->addScalarResult('pseudonym', 'pseudonym', 'string')
158 ->addScalarResult('g_id', 'g_id', 'integer')
159 ->addScalarResult('g_title', 'g_title', 'string');
163 ->createNativeQuery($req, $rsm)
170 foreach($res as $data) {
171 //Get translated group
172 $group = $translator->trans($data['g_title']);
174 //Init group subarray
175 if (!isset($ret[$group])) {
180 $ret[$group][$data['id']] = [
181 'mail' => $data['mail'],
182 'pseudonym' => $data['pseudonym']