diff options
Diffstat (limited to 'src/model/Model.php')
| -rw-r--r-- | src/model/Model.php | 14 |
1 files changed, 5 insertions, 9 deletions
diff --git a/src/model/Model.php b/src/model/Model.php index ad9e6c7..9effbe7 100644 --- a/src/model/Model.php +++ b/src/model/Model.php | |||
| @@ -3,7 +3,7 @@ | |||
| 3 | 3 | ||
| 4 | class Model extends DB | 4 | class Model extends DB |
| 5 | { | 5 | { |
| 6 | private $db; // instance de PDO | 6 | protected $db; // instance de PDO |
| 7 | protected $table; // <= enfant | 7 | protected $table; // <= enfant |
| 8 | //static protected $tableStructure; | 8 | //static protected $tableStructure; |
| 9 | 9 | ||
| @@ -12,7 +12,6 @@ class Model extends DB | |||
| 12 | $this->db = parent::getInstance(); // connexion | 12 | $this->db = parent::getInstance(); // connexion |
| 13 | } | 13 | } |
| 14 | 14 | ||
| 15 | |||
| 16 | // setters (plusieurs en même temps) | 15 | // setters (plusieurs en même temps) |
| 17 | public function hydrate(array $data) // $data = tableau associatif en entrée: nom_du_champ => valeur | 16 | public function hydrate(array $data) // $data = tableau associatif en entrée: nom_du_champ => valeur |
| 18 | { | 17 | { |
| @@ -51,7 +50,7 @@ class Model extends DB | |||
| 51 | } | 50 | } |
| 52 | 51 | ||
| 53 | 52 | ||
| 54 | // méthodes CRUD qui marchent (les spécifiques sont dans les classes enfant) | 53 | // méthodes CRUD |
| 55 | 54 | ||
| 56 | // create INSERT | 55 | // create INSERT |
| 57 | public function create() // = write | 56 | public function create() // = write |
| @@ -79,17 +78,17 @@ class Model extends DB | |||
| 79 | 78 | ||
| 80 | 79 | ||
| 81 | // read SELECT | 80 | // read SELECT |
| 82 | public function readAll() | 81 | public function readAll() // obtenir une table |
| 83 | { | 82 | { |
| 84 | $query = $this->execQuery('SELECT * FROM ' . $this->table . ';'); // fonctionne aussi sans le point virgule dans le SQL!! | 83 | $query = $this->execQuery('SELECT * FROM ' . $this->table . ';'); // fonctionne aussi sans le point virgule dans le SQL!! |
| 85 | return($query->fetchAll()); | 84 | return($query->fetchAll()); |
| 86 | } | 85 | } |
| 87 | public function findById(int $id) | 86 | public function findById(int $id) // obtenir une entrée avec son ID |
| 88 | { | 87 | { |
| 89 | return($this->execQuery('SELECT * FROM ' . $this->table . ' WHERE id = ' . $id)->fetch()); | 88 | return($this->execQuery('SELECT * FROM ' . $this->table . ' WHERE id = ' . $id)->fetch()); |
| 90 | } | 89 | } |
| 91 | 90 | ||
| 92 | public function find(array $criteria) | 91 | public function find(array $criteria) // obtenir une entrée avec un tableau associatif 'champ' => 'valeur' |
| 93 | { | 92 | { |
| 94 | $fields = []; | 93 | $fields = []; |
| 95 | $values = []; | 94 | $values = []; |
| @@ -140,7 +139,6 @@ class Model extends DB | |||
| 140 | // DBStructure::${self::$tableStructure} permet de nommer une variable statique de classe | 139 | // DBStructure::${self::$tableStructure} permet de nommer une variable statique de classe |
| 141 | static public function createTables() | 140 | static public function createTables() |
| 142 | { | 141 | { |
| 143 | //~ var_dump(StructTablesDB::$structureOfTables); | ||
| 144 | foreach(StructTablesDB::$structureOfTables as $tableName => $oneTable) | 142 | foreach(StructTablesDB::$structureOfTables as $tableName => $oneTable) |
| 145 | { | 143 | { |
| 146 | //var_dump(StructTablesDB::${self::$tableStructure}); => propriété statique de classe dans une variable | 144 | //var_dump(StructTablesDB::${self::$tableStructure}); => propriété statique de classe dans une variable |
| @@ -156,7 +154,5 @@ class Model extends DB | |||
| 156 | 154 | ||
| 157 | parent::getInstance()->exec($query); // merci singleton! | 155 | parent::getInstance()->exec($query); // merci singleton! |
| 158 | } | 156 | } |
| 159 | |||
| 160 | |||
| 161 | } | 157 | } |
| 162 | } | 158 | } |
