]> Raphaƫl G. Git Repositories - airbundle/blob - Repository/DanceRepository.php
Add strict
[airbundle] / Repository / DanceRepository.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 * DanceRepository
10 */
11 class DanceRepository extends \Doctrine\ORM\EntityRepository {
12 /**
13 * Find dances by user id
14 *
15 * @param $id The user id
16 * @return array The user dances
17 */
18 public function findByUserId($userId) {
19 //Get entity manager
20 $em = $this->getEntityManager();
21
22 //Get quote strategy
23 $qs = $em->getConfiguration()->getQuoteStrategy();
24 $dp = $em->getConnection()->getDatabasePlatform();
25
26 //Get quoted table names
27 //XXX: this allow to make this code table name independent
28 $tables = [
29 'RapsysAirBundle:UserDance' => $qs->getJoinTableName($em->getClassMetadata('RapsysAirBundle:User')->getAssociationMapping('dances'), $em->getClassMetadata('RapsysAirBundle:User'), $dp),
30 'RapsysAirBundle:Dance' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Dance'), $dp)
31 ];
32
33 //Set the request
34 $req = 'SELECT d.id, d.title
35 FROM RapsysAirBundle:UserDance AS ud
36 JOIN RapsysAirBundle:Dance AS d ON (d.id = ud.dance_id)
37 WHERE ud.user_id = :uid';
38
39 //Replace bundle entity name by table name
40 $req = str_replace(array_keys($tables), array_values($tables), $req);
41
42 //Get result set mapping instance
43 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
44 $rsm = new ResultSetMapping();
45
46 //Declare result set for our request
47 $rsm->addEntityResult('RapsysAirBundle:Dance', 'd');
48 $rsm->addFieldResult('d', 'id', 'id');
49 $rsm->addFieldResult('d', 'title', 'title');
50
51 //Send result
52 return $em
53 ->createNativeQuery($req, $rsm)
54 ->setParameter('uid', $userId)
55 ->getResult();
56 }
57 }