diff options
Diffstat (limited to 'src/model/Model.php')
| -rw-r--r-- | src/model/Model.php | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/src/model/Model.php b/src/model/Model.php index 8cbf056..938e3dd 100644 --- a/src/model/Model.php +++ b/src/model/Model.php | |||
| @@ -9,7 +9,8 @@ class Model extends DB | |||
| 9 | 9 | ||
| 10 | public function __construct() | 10 | public function __construct() |
| 11 | { | 11 | { |
| 12 | $this->db = parent::getInstance(); // connexion | 12 | //~ $this->db = parent::getInstance(); // connexion |
| 13 | //~ $this->table = strtolower(__CLASS__); | ||
| 13 | } | 14 | } |
| 14 | 15 | ||
| 15 | // getters | 16 | // getters |
| @@ -26,8 +27,9 @@ class Model extends DB | |||
| 26 | // nom d'un setter, forme "setMachin()" | 27 | // nom d'un setter, forme "setMachin()" |
| 27 | $setterName = 'set' . ucfirst($key); // ucfirst met la première lettre en majuscule | 28 | $setterName = 'set' . ucfirst($key); // ucfirst met la première lettre en majuscule |
| 28 | // détection | 29 | // détection |
| 29 | if(method_exists($this, $setterName)) // on trouve aussi la méthode is_callable() | 30 | if(method_exists($this, $setterName) && $value != NULL) // on trouve aussi la méthode is_callable() |
| 30 | { | 31 | { |
| 32 | //~ var_dump($value); | ||
| 31 | // on renseigne les propriétés des l'instance | 33 | // on renseigne les propriétés des l'instance |
| 32 | $this->$setterName($value); // nom d'une méthode dans une variable | 34 | $this->$setterName($value); // nom d'une méthode dans une variable |
| 33 | } | 35 | } |
| @@ -84,7 +86,7 @@ class Model extends DB | |||
| 84 | 86 | ||
| 85 | 87 | ||
| 86 | // read SELECT | 88 | // read SELECT |
| 87 | public function readAll(): array // obtenir une table | 89 | protected function readAll(): array // obtenir une table |
| 88 | { | 90 | { |
| 89 | return($this->execQuery('SELECT * FROM ' . $this->table)->fetchAll()); // fonctionne aussi sans le point virgule dans le SQL!! | 91 | return($this->execQuery('SELECT * FROM ' . $this->table)->fetchAll()); // fonctionne aussi sans le point virgule dans le SQL!! |
| 90 | } | 92 | } |
| @@ -94,7 +96,7 @@ class Model extends DB | |||
| 94 | return($this->execQuery('SELECT * FROM ' . $this->table . ' WHERE id = ' . $id)->fetch()); | 96 | return($this->execQuery('SELECT * FROM ' . $this->table . ' WHERE id = ' . $id)->fetch()); |
| 95 | } | 97 | } |
| 96 | 98 | ||
| 97 | public function find(array $criteria): array // obtenir une entrée avec un tableau associatif 'champ' => 'valeur' | 99 | protected function find(array $criteria): array // obtenir une entrée avec un tableau associatif 'champ' => 'valeur' |
| 98 | { | 100 | { |
| 99 | $fields = []; | 101 | $fields = []; |
| 100 | $values = []; | 102 | $values = []; |
| @@ -134,7 +136,7 @@ class Model extends DB | |||
| 134 | 136 | ||
| 135 | 137 | ||
| 136 | // delete DELETE | 138 | // delete DELETE |
| 137 | public function delete(int $id) | 139 | protected function delete(int $id) |
| 138 | { | 140 | { |
| 139 | return($this->execQuery("DELETE FROM {$this->table} WHERE id = ?", [$id])); // double quotes "" pour insertion de variable, paramètre [$id] parce qu'on veut un tableau | 141 | return($this->execQuery("DELETE FROM {$this->table} WHERE id = ?", [$id])); // double quotes "" pour insertion de variable, paramètre [$id] parce qu'on veut un tableau |
| 140 | } | 142 | } |
