corso https://vola.udemy.com/course/php-mvc-from-scratch/learn/lecture/40931984#overview
filippo.bertilotti
2024-05-24 e66fc13048b8a04c748c34711629f4a3bd50c71f
commit | author | age
766451 1 <?php
F 2     namespace Framework;
3
4     use App\Database;
5     use PDO;
6     abstract class Model
7     {
4dbbdf 8         protected array $errors = [];
d7a326 9         protected ?string $table;
4dbbdf 10
F 11         abstract protected function validate (array $data): void;
12         
e66fc1 13         public  function __construct(protected Database $database) {
4dbbdf 14         }
F 15
bd886f 16         public function update(string $id, array $data): bool {
F 17             $this->validate($data);
18             
19             if(!empty($this->errors)) {
20                 return false;
21             }
22
606d10 23             $sql = "UPDATE {$this->getTable()}";
F 24
25             unset($data["id"]);
26
27             $assignments = array_keys($data);
28
29             array_walk($assignments, function (&$value) {
30                 $value = "$value = ?";
31             });
32
33             $sql .= " SET " . implode(", ", $assignments);
34             $sql .= " WHERE id = ?";
35
36             $conn = $this->database->getConnection();
37             $stmt = $conn->prepare($sql);
38
39             $i = 1;
40             foreach ($data as $value) {
41                 $type = match(gettype($value)) {
42                     "boolean" => PDO::PARAM_BOOL,
43                     "integer"=> PDO::PARAM_INT,
44                     "NULL"=> PDO::PARAM_NULL,
45                     default => PDO::PARAM_STR
46                 };
47
48                 
49                 $stmt->bindValue($i++, $value, $type);
50             }
51
52             $stmt->bindValue($i, $id, PDO::PARAM_INT);
53             return $stmt->execute();
bd886f 54
F 55         }
4dbbdf 56
F 57         protected function addError(string $field, string $message): void {
58             $this->errors[$field] = $message;
59         }
60
61         public function getErrors(): array {
62             return $this->errors;
63         }
766451 64
a26040 65         private function getTable() {
F 66             $parts = explode("\\", $this::class);
67             return strtolower(array_pop($parts));
766451 68         }
6704d0 69         public function getInsertID(): string {
F 70             $conn = $this->database->getConnection();
71             return $conn->lastInsertId();
72         }
766451 73         public function findAll(): array|bool
F 74         {
75             $pdo = $this->database->getConnection();
76
a26040 77             $sql = "SELECT * FROM {$this->getTable()}";
d7a326 78
F 79             $stmt = $pdo->query($sql);
766451 80
F 81             return $stmt->fetchAll(PDO::FETCH_ASSOC);
82         }
83
84         public function find(string $id) {
85             $conn = $this->database->getConnection();
86             $sql = "SELECT *
a26040 87                     FROM {$this->getTable()}
766451 88                     WHERE id = :id";
F 89             $stmt = $conn->prepare($sql);
90             $stmt->bindValue(":id", $id, PDO::PARAM_INT);
91             $stmt->execute();
92             return $stmt->fetch(PDO::FETCH_ASSOC);
93         }
fc5ee7 94
F 95         public function insert(array $data) : bool {
4dbbdf 96             $this->validate($data);
F 97             
98             if(!empty($this->errors)) {
7a2549 99                 return false;
F 100             }
6f90bf 101             $columns = implode(", " , array_keys($data));
F 102             $placeholders = implode(", ", array_fill(0, count($data), "?"));
103
104             $sql = "INSERT INTO {$this->getTable()} ($columns) VALUES ($placeholders)";
105
fc5ee7 106             $conn = $this->database->getConnection();
F 107             $stmt = $conn->prepare($sql);
b1d4d1 108
F 109             $i = 1;
110             foreach ($data as $value) {
111                 $type = match(gettype($value)) {
112                     "boolean" => PDO::PARAM_BOOL,
113                     "integer"=> PDO::PARAM_INT,
114                     "NULL"=> PDO::PARAM_NULL,
115                     default => PDO::PARAM_STR
116                 };
117
118                 
606d10 119                 $stmt->bindValue($i++, $value, $type);
b1d4d1 120             }
F 121             
fc5ee7 122
F 123             return $stmt->execute();
124         }
a500c4 125         
F 126         public function delete(string $id): bool {
127             $sql = "DELETE FROM {$this->getTable()} WHERE id = :id";
766451 128
a500c4 129             $conn = $this->database->getConnection();
F 130             $stmt = $conn->prepare($sql);
131             $stmt->bindValue(":id", $id, PDO::PARAM_INT);
766451 132
a500c4 133             return $stmt->execute();
F 134         }
135     }