Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
45.83% |
11 / 24 |
|
40.00% |
2 / 5 |
CRAP | |
0.00% |
0 / 1 |
| CastingRepository | |
45.83% |
11 / 24 |
|
40.00% |
2 / 5 |
14.79 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| add | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| remove | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| findByMovieOrderByCreditOrderWithPerson | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
| findByMovieOrderByCreditOrderQB | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Repository; |
| 4 | |
| 5 | use App\Entity\Casting; |
| 6 | use App\Entity\Movie; |
| 7 | use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; |
| 8 | use Doctrine\Persistence\ManagerRegistry; |
| 9 | |
| 10 | /** |
| 11 | * @extends ServiceEntityRepository<Casting> |
| 12 | * |
| 13 | * @method Casting|null find($id, $lockMode = null, $lockVersion = null) |
| 14 | * @method Casting|null findOneBy(array $criteria, array $orderBy = null) |
| 15 | * @method Casting[] findAll() |
| 16 | * @method Casting[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) |
| 17 | */ |
| 18 | class CastingRepository extends ServiceEntityRepository |
| 19 | { |
| 20 | public function __construct(ManagerRegistry $registry) |
| 21 | { |
| 22 | parent::__construct($registry, Casting::class); |
| 23 | } |
| 24 | |
| 25 | public function add(Casting $entity, bool $flush = false): void |
| 26 | { |
| 27 | $this->getEntityManager()->persist($entity); |
| 28 | |
| 29 | if ($flush) { |
| 30 | $this->getEntityManager()->flush(); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | public function remove(Casting $entity, bool $flush = false): void |
| 35 | { |
| 36 | $this->getEntityManager()->remove($entity); |
| 37 | |
| 38 | if ($flush) { |
| 39 | $this->getEntityManager()->flush(); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | public function findByMovieOrderByCreditOrderWithPerson(Movie $movie) |
| 44 | { |
| 45 | // 1. findBy() |
| 46 | // * $castingRepository->findBy(["movie" => $movie],["creditOrder" => "ASC"]); |
| 47 | |
| 48 | // 2. Doctrine génère une requete DQL : Doctrine Query Language |
| 49 | // ? https://www.doctrine-project.org/projects/doctrine-orm/en/2.14/reference/dql-doctrine-query-language.html#doctrine-query-language |
| 50 | // ex : SELECT u FROM MyProject\Model\User u WHERE u.age > 20 |
| 51 | // * SELECT casting FROM App\Entity\Casting casting WHERE casting.movie = $movie ORDER BY casting.creditOrder ASC |
| 52 | // SELECT <Alias de l'entité> |
| 53 | // FROM <FQCN de l'entité> <Alias de l'entité> |
| 54 | // WHERE <Alias de l'entité>.<propriété> = $valeur |
| 55 | // ORDER BY <Alias de l'entité>.<propriété> <ASC/DESC> |
| 56 | |
| 57 | // * SELECT u, a FROM User u JOIN u.address a WHERE a.city = 'Berlin'" |
| 58 | // JOIN <Alias de l'entité>.<propriété de jointure> <Alias de l'entité de la jointure> |
| 59 | // * si on veux récupérer les entités de la jointure, il faut ajouter l'alias dans le select |
| 60 | // SELECT <Alias de l'entité>, <Alias de l'entité de la jointure> |
| 61 | |
| 62 | $em = $this->getEntityManager(); |
| 63 | // on construit la requete DQL |
| 64 | // ! Object of class App\Entity\Movie could not be converted to string |
| 65 | // comme on construit une chaine de caractère, on ne peut pas donner directement l'objets |
| 66 | // on passe donc par l'ID |
| 67 | $movieId = $movie->getId(); |
| 68 | |
| 69 | $query = $em->createQuery(" |
| 70 | SELECT casting, person |
| 71 | FROM App\Entity\Casting casting |
| 72 | JOIN casting.person person |
| 73 | WHERE casting.movie = $movieId |
| 74 | ORDER BY casting.creditOrder ASC |
| 75 | "); |
| 76 | |
| 77 | // version avec paramètre, comme requete préparées |
| 78 | $queryNamedParam = $em->createQuery(" |
| 79 | SELECT casting, person |
| 80 | FROM App\Entity\Casting casting |
| 81 | JOIN casting.person person |
| 82 | WHERE casting.movie = :movieobject |
| 83 | ORDER BY casting.creditOrder ASC |
| 84 | "); |
| 85 | // ici on peut fournir l'objet plutot que de passer par l'ID |
| 86 | $queryNamedParam->setParameter('movieobject', $movie); |
| 87 | |
| 88 | // on éxécute la requete |
| 89 | $result = $query->getResult(); |
| 90 | // dd($result); |
| 91 | // on reçoit un tableau d'objet Casting |
| 92 | |
| 93 | // 3. Doctrine génère le SQL |
| 94 | // SELECT * FROM casting WHERE movie_id = xx ORBER BY credit_order ASC |
| 95 | |
| 96 | return $result; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @return Casting[] Returns an array of Casting objects |
| 101 | */ |
| 102 | public function findByMovieOrderByCreditOrderQB(Movie $movie): array |
| 103 | { |
| 104 | // même chose que le findBy() |
| 105 | // avec l'objet de création de Query : QueryBuilder |
| 106 | return $this->createQueryBuilder('c') |
| 107 | ->andWhere('c.movie = :movieobject') |
| 108 | ->setParameter('movieobject', $movie) |
| 109 | ->orderBy('c.creditOrder', 'ASC') |
| 110 | ->getQuery() |
| 111 | ->getResult() |
| 112 | ; |
| 113 | } |
| 114 | |
| 115 | |
| 116 | // public function findOneBySomeField($value): ?Casting |
| 117 | // { |
| 118 | // return $this->createQueryBuilder('c') |
| 119 | // ->andWhere('c.exampleField = :val') |
| 120 | // ->setParameter('val', $value) |
| 121 | // ->getQuery() |
| 122 | // ->getOneOrNullResult() |
| 123 | // ; |
| 124 | // } |
| 125 | } |