]> Raphaël G. Git Repositories - airbundle/blob - Repository/SlotRepository.php
Add missing translation
[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 use Rapsys\AirBundle\Repository;
17
18 /**
19 * SlotRepository
20 */
21 class SlotRepository extends Repository {
22 /**
23 * Find slots with translated title
24 *
25 * @return array The slots id keyed by translated title
26 */
27 public function findAllWithTranslatedTitle(): array {
28 //Set the request from quoted table name
29 //XXX: this allow to make this code table name independent
30 $req = 'SELECT s.id, s.title FROM Rapsys\AirBundle\Entity\Slot AS s';
31
32 //Replace bundle entity name by table name
33 $req = str_replace($this->tableKeys, $this->tableValues, $req);
34
35 //Get result set mapping instance
36 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
37 $rsm = new ResultSetMapping();
38
39 //Declare all fields
40 //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
41 //addScalarResult($sqlColName, $resColName, $type = 'string');
42 $rsm->addScalarResult('id', 'id', 'integer')
43 ->addScalarResult('title', 'title', 'string')
44 ->addIndexByScalar('id');
45
46 //Fetch result
47 $res = $this->_em
48 ->createNativeQuery($req, $rsm)
49 ->getResult();
50
51 //Init return
52 $ret = [];
53
54 //Process result
55 foreach($res as $data) {
56 //Get translated slot
57 $slot = $this->translator->trans($data['title']);
58 //Set data
59 //XXX: ChoiceType use display string as key
60 $ret[$slot] = $data['id'];
61 }
62
63 //Send result
64 return $ret;
65 }
66 }