]> Raphaël G. Git Repositories - airbundle/blob - Repository/SlotRepository.php
Add country, city and zipcode member
[airbundle] / Repository / SlotRepository.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys AirBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Rapsys\AirBundle\Repository;
13
14 use Doctrine\ORM\Query\ResultSetMapping;
15
16 /**
17 * SlotRepository
18 */
19 class SlotRepository extends EntityRepository {
20 /**
21 * Find slots with translated title
22 *
23 * @return array The slots id keyed by translated title
24 */
25 public function findAllWithTranslatedTitle(): array {
26 //Set the request from quoted table name
27 //XXX: this allow to make this code table name independent
28 $req = 'SELECT s.id, s.title FROM RapsysAirBundle:Slot AS s';
29
30 //Replace bundle entity name by table name
31 $req = str_replace($this->tableKeys, $this->tableValues, $req);
32
33 //Get result set mapping instance
34 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
35 $rsm = new ResultSetMapping();
36
37 //Declare all fields
38 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
39 //addScalarResult($sqlColName, $resColName, $type = 'string');
40 $rsm->addScalarResult('id', 'id', 'integer')
41 ->addScalarResult('title', 'title', 'string')
42 ->addIndexByScalar('id');
43
44 //Fetch result
45 $res = $this->_em
46 ->createNativeQuery($req, $rsm)
47 ->getResult();
48
49 //Init return
50 $ret = [];
51
52 //Process result
53 foreach($res as $data) {
54 //Get translated slot
55 $slot = $this->translator->trans($data['title']);
56 //Set data
57 //XXX: ChoiceType use display string as key
58 $ret[$slot] = $data['id'];
59 }
60
61 //Send result
62 return $ret;
63 }
64 }