]> Raphaƫl G. Git Repositories - airbundle/blob - Repository/SlotRepository.php
Renamed in Handler
[airbundle] / Repository / SlotRepository.php
1 <?php
2
3 namespace Rapsys\AirBundle\Repository;
4
5 use Symfony\Component\Translation\TranslatorInterface;
6 use Doctrine\ORM\Query\ResultSetMapping;
7
8 /**
9 * SlotRepository
10 */
11 class SlotRepository extends \Doctrine\ORM\EntityRepository {
12 /**
13 * Find slots with translated title
14 *
15 * @param $translator The TranslatorInterface instance
16 */
17 public function findAllWithTranslatedTitle(TranslatorInterface $translator) {
18 //Get entity manager
19 $em = $this->getEntityManager();
20
21 //Get quote strategy
22 $qs = $em->getConfiguration()->getQuoteStrategy();
23 $dp = $em->getConnection()->getDatabasePlatform();
24
25 //Set the request from quoted table name
26 //XXX: this allow to make this code table name independent
27 $req = 'SELECT s.id, s.title FROM '.$qs->getTableName($em->getClassMetadata('RapsysAirBundle:Slot'), $dp).' AS s';
28
29 //Get result set mapping instance
30 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
31 $rsm = new ResultSetMapping();
32
33 //Declare all fields
34 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
35 //addScalarResult($sqlColName, $resColName, $type = 'string');
36 $rsm->addScalarResult('id', 'id', 'integer')
37 ->addScalarResult('title', 'title', 'string')
38 ->addIndexByScalar('id');
39
40 //Fetch result
41 $res = $em
42 ->createNativeQuery($req, $rsm)
43 ->getResult();
44
45 //Init return
46 $ret = [];
47
48 //Process result
49 foreach($res as $data) {
50 //Get translated slot
51 $slot = $translator->trans($data['title']);
52 //Set data
53 //XXX: ChoiceType use display string as key
54 $ret[$slot] = $data['id'];
55 }
56
57 //Send result
58 return $ret;
59 }
60 }