Paaxio 1.0
Plateforme de streaming musical - SAE IUT Bayonne
Chargement...
Recherche...
Aucune correspondance
fichier.dao.php
Aller à la documentation de ce fichier.
1<?php
7class FichierDAO {
11 private ?PDO $pdo;
12
17 public function __construct(?PDO $pdo = null)
18 {
19 $this->pdo = $pdo;
20 }
21
26 public function findAll(): array
27 {
28 $sql = "SELECT * FROM fichier";
29 $pdoStatement = $this->pdo->prepare($sql);
30 $pdoStatement->execute();
31 $pdoStatement->setFetchMode(PDO::FETCH_ASSOC);
32 $tableau = $pdoStatement->fetchAll();
33 $fichier = $this->hydrateMany($tableau);
34 return $fichier;
35 }
36
37 public function find(int $id): Fichier
38 {
39 $sql = "SELECT * FROM fichier WHERE idFichier = :id";
40 $pdoStatement = $this->pdo->prepare($sql);
41 $pdoStatement->execute(array(
42 ':id' => $id
43 ));
44
45 $pdoStatement->setFetchMode(PDO::FETCH_ASSOC);
46 $tableau = $pdoStatement->fetch();
47 $fichier = $this->hydrate($tableau);
48 return $fichier;
49 }
50
51 public function hydrate(array $tableaAssoc): Fichier
52 {
53 $fichier = new Fichier();
54 $fichier->setUrlFichier(isset($tableaAssoc['urlFichier']) ? (int)$tableaAssoc['urlFichier'] : null);
55
56 // Conversion du type (enum)
57 if (!empty($tableaAssoc['typeFichier'])) {
58 $fichier->setTypeFichier(TypeFichier::from($tableaAssoc['typeFichier']));
59 }
60 // Conversion du format (enum)
61 if (!empty($tableaAssoc['formatFichier'])) {
62 $fichier->setFormatFichier(FormatFichier::from($tableaAssoc['formatFichier']));
63 }
64
65 // Conversion sécurisée des dates SQL → objets DateTime
66 $fichier->setDateAjout(
67 !empty($tableaAssoc['dateAjout']) ? new DateTime($tableaAssoc['dateAjout']) : null
68 );
69
70 return $fichier;
71 }
72
73 public function hydrateMany(array $tableauxAssoc): array
74 {
75 $fichiers = [];
76 foreach ($tableauxAssoc as $tableauAssoc) {
77 $fichiers[] = $this->hydrate($tableauAssoc);
78 }
79 return $fichiers;
80 }
81
85 public function getPdo(): ?PDO
86 {
87 return $this->pdo;
88 }
93 public function setPdo(?PDO $pdo): void
94 {
95 $this->pdo = $pdo;
96 }
97}
__construct(?PDO $pdo=null)
Constructeur de la classe FichierDAO.
setPdo(?PDO $pdo)
Set the value of pdo.
hydrateMany(array $tableauxAssoc)
find(int $id)
hydrate(array $tableaAssoc)
findAll()
Récupère tous les fichiers de la base de données.
getPdo()
Get the value of pdo.