Paaxio 1.0
Plateforme de streaming musical - SAE IUT Bayonne
Chargement...
Recherche...
Aucune correspondance
genre.dao.php
Aller à la documentation de ce fichier.
1<?php
8{
12 private ?PDO $pdo;
13
18 public function __construct(?PDO $pdo = null)
19 {
20 $this->pdo = $pdo;
21 }
22
27 public function findAll(): array
28 {
29 $sql = "SELECT * FROM genre";
30 $pdoStatement = $this->pdo->prepare($sql);
31 $pdoStatement->execute();
32 $pdoStatement->setFetchMode(PDO::FETCH_ASSOC);
33 $tableau = $pdoStatement->fetchAll();
34 $genre = $this->hydrateMany($tableau);
35 return $genre;
36 }
37
38 public function find(int $id): ?Genre
39 {
40 $sql = "SELECT * FROM genre WHERE idGenre = :id";
41 $pdoStatement = $this->pdo->prepare($sql);
42 $pdoStatement->execute([
43 ':id' => $id
44 ]);
45
46 $pdoStatement->setFetchMode(PDO::FETCH_ASSOC);
47 $tableau = $pdoStatement->fetch();
48 if (!$tableau) {
49 return null;
50 }
51 $genre = $this->hydrate($tableau);
52 return $genre;
53 }
54
55 public function findByName(string $nomGenre): ?Genre
56 {
57 $sql = "SELECT * FROM genre WHERE nomGenre = :nomGenre";
58 $pdoStatement = $this->pdo->prepare($sql);
59 $pdoStatement->execute([
60 ':nomGenre' => $nomGenre
61 ]);
62
63 $pdoStatement->setFetchMode(PDO::FETCH_ASSOC);
64 $tableau = $pdoStatement->fetch();
65 if (!$tableau) {
66 return null;
67 }
68 $genre = $this->hydrate($tableau);
69 return $genre;
70 }
71
72 public function create(string $nomGenre): int
73 {
74 $sql = "INSERT INTO genre (nomGenre) VALUES (:nomGenre)";
75 $pdoStatement = $this->pdo->prepare($sql);
76 $pdoStatement->execute([
77 ':nomGenre' => $nomGenre
78 ]);
79
80 return (int)$this->pdo->lastInsertId();
81 }
82
83 public function findOrCreateByName(string $nomGenre): ?Genre
84 {
85 // Si le nom du genre est vide, on ne fait rien et on retourne null.
86 if (trim($nomGenre) === '') {
87 return null;
88 }
89
90 // On cherche d'abord si le genre existe.
91 $genre = $this->findByName($nomGenre);
92
93 if ($genre) {
94 // S'il existe, on le retourne.
95 return $genre;
96 } else {
97 // Sinon, on le crée...
98 $idNouveauGenre = $this->create($nomGenre);
99 // ...et on retourne le nouvel objet Genre.
100 return $this->find($idNouveauGenre);
101 }
102 }
103
104 public function rechercherParNom(string $nom): array
105 {
106 $sql = "SELECT * FROM genre WHERE nomGenre LIKE :nom";
107 $pdoStatement = $this->pdo->prepare($sql);
108 $likeNom = '%' . $nom . '%';
109 $pdoStatement->bindParam(':nom', $likeNom, PDO::PARAM_STR);
110 $pdoStatement->execute();
111 $pdoStatement->setFetchMode(PDO::FETCH_ASSOC);
112 $tableau = $pdoStatement->fetchAll();
113 $genres = $this->hydrateMany($tableau);
114 return $genres;
115 }
116
117 public function hydrate(array $tableaAssoc): genre
118 {
119 $genre = new Genre();
120 $genre->setIdGenre(isset($tableaAssoc['idGenre']) ? (int)$tableaAssoc['idGenre'] : null);
121 $genre->setNomGenre($tableaAssoc['nomGenre'] ?? null);
122 return $genre;
123 }
124
125 public function hydrateMany(array $tableauxAssoc): array
126 {
127 $genres = [];
128 foreach ($tableauxAssoc as $tableauAssoc) {
129 $genres[] = $this->hydrate($tableauAssoc);
130 }
131 return $genres;
132 }
133
137 public function getPdo(): ?PDO
138 {
139 return $this->pdo;
140 }
145 public function setPdo(?PDO $pdo): void
146 {
147 $this->pdo = $pdo;
148 }
149}
create(string $nomGenre)
Definition genre.dao.php:72
getPdo()
Get the value of pdo.
find(int $id)
Definition genre.dao.php:38
setPdo(?PDO $pdo)
Set the value of pdo.
hydrate(array $tableaAssoc)
findOrCreateByName(string $nomGenre)
Definition genre.dao.php:83
rechercherParNom(string $nom)
__construct(?PDO $pdo=null)
Constructeur de la classe GenreDAO.
Definition genre.dao.php:18
findByName(string $nomGenre)
Definition genre.dao.php:55
hydrateMany(array $tableauxAssoc)
findAll()
Récupère tous les genres de la base de données.
Definition genre.dao.php:27
$genres
Definition include.php:308