Paaxio 1.0
Plateforme de streaming musical - SAE IUT Bayonne
Chargement...
Recherche...
Aucune correspondance
role.dao.php
Aller à la documentation de ce fichier.
1<?php
2
8class RoleDAO
9{
13 private ?PDO $pdo;
14
19 public function __construct(?PDO $pdo = null)
20 {
21 $this->pdo = $pdo;
22 }
23
28 public function findAll(): array
29 {
30 $sql = "SELECT * FROM role";
31 $pdoStatement = $this->pdo->prepare($sql);
32 $pdoStatement->execute();
33 $pdoStatement->setFetchMode(PDO::FETCH_ASSOC);
34 $tableau = $pdoStatement->fetchAll();
35 $roles = $this->hydrateMany($tableau);
36 return $roles;
37 }
38
39 public function find(int $id): Role
40 {
41 $sql = "SELECT * FROM role WHERE idRole = :id";
42 $pdoStatement = $this->pdo->prepare($sql);
43 $pdoStatement->execute(array(
44 ':id' => $id
45 ));
46
47 $pdoStatement->setFetchMode(PDO::FETCH_ASSOC);
48 $tableau = $pdoStatement->fetch();
49 $role = $this->hydrate($tableau);
50 return $role;
51 }
52
53 public function findByType(string $typeRole): ?Role
54 {
55 $sql = "SELECT * FROM role WHERE typeRole = :typeRole LIMIT 1";
56 $pdoStatement = $this->pdo->prepare($sql);
57 $pdoStatement->execute([
58 ':typeRole' => $typeRole
59 ]);
60 $pdoStatement->setFetchMode(PDO::FETCH_ASSOC);
61 $tableau = $pdoStatement->fetch();
62
63 if (!$tableau) {
64 return null;
65 }
66
67 return $this->hydrate($tableau);
68 }
69
70 public function hydrate(array $tableaAssoc): Role
71 {
72 $role = new Role();
73 $role->setIdRole(isset($tableaAssoc['idRole']) ? (int)$tableaAssoc['idRole'] : null);
74 $role->setTypeRole($tableaAssoc['typeRole'] ?? null);
75 $role->setLibelleRole($tableaAssoc['libelleRole'] ?? null);
76 return $role;
77 }
78
79 public function hydrateMany(array $tableauxAssoc): array
80 {
81 $roles = [];
82 foreach ($tableauxAssoc as $tableauAssoc) {
83 $roles[] = $this->hydrate($tableauAssoc);
84 }
85 return $roles;
86 }
87
88 public function getPDO(): ?PDO
89 {
90 return $this->pdo;
91 }
92
93 public function setPDO($pdo): void
94 {
95 $this->pdo = $pdo;
96 }
97}
setPDO($pdo)
Definition role.dao.php:93
PDO $pdo
Definition role.dao.php:13
find(int $id)
Definition role.dao.php:39
hydrate(array $tableaAssoc)
Definition role.dao.php:70
__construct(?PDO $pdo=null)
Constructeur de la classe RoleDAO.
Definition role.dao.php:19
findAll()
Récupère tous les rôles de la base de données.
Definition role.dao.php:28
hydrateMany(array $tableauxAssoc)
Definition role.dao.php:79
findByType(string $typeRole)
Definition role.dao.php:53