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 title
15 * @param $translator The TranslatorInterface instance
17 public function findAllWithTranslatedGroupAndTitle(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:GroupUser' => $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:Title' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Title'), $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:Title AS t ON (t.id = u.title_id)
39 LEFT JOIN RapsysAirBundle:GroupUser 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']?:'ROLE_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'];