diff options
Diffstat (limited to 'src/model')
| -rw-r--r-- | src/model/Clients.php | 12 | ||||
| -rw-r--r-- | src/model/Model.php | 37 |
2 files changed, 30 insertions, 19 deletions
diff --git a/src/model/Clients.php b/src/model/Clients.php index a5cc276..0f70eb6 100644 --- a/src/model/Clients.php +++ b/src/model/Clients.php | |||
| @@ -124,6 +124,18 @@ class Clients extends Model | |||
| 124 | $this->type = (string) $value; | 124 | $this->type = (string) $value; |
| 125 | return $this; | 125 | return $this; |
| 126 | } | 126 | } |
| 127 | public function typeToClient(): bool | ||
| 128 | { | ||
| 129 | if($this->type != 'client') | ||
| 130 | { | ||
| 131 | $this->type = 'client'; | ||
| 132 | return true; | ||
| 133 | } | ||
| 134 | else | ||
| 135 | { | ||
| 136 | return false; | ||
| 137 | } | ||
| 138 | } | ||
| 127 | 139 | ||
| 128 | 140 | ||
| 129 | public function findByKeywords(array $keywords, string $field): array // n'hydrate pas les variables, on doit choisir un client et hydrater ensuite | 141 | public function findByKeywords(array $keywords, string $field): array // n'hydrate pas les variables, on doit choisir un client et hydrater ensuite |
diff --git a/src/model/Model.php b/src/model/Model.php index 6c799c3..1fdab7d 100644 --- a/src/model/Model.php +++ b/src/model/Model.php | |||
| @@ -5,13 +5,9 @@ abstract class Model extends DB | |||
| 5 | { | 5 | { |
| 6 | protected $db; // instance de PDO | 6 | protected $db; // instance de PDO |
| 7 | protected $table; // <= enfant | 7 | protected $table; // <= enfant |
| 8 | //static protected $tableStructure; | ||
| 9 | 8 | ||
| 10 | //~ public function __construct() // à surcharger | 9 | //~ protected function __construct() // pour appel avec parent::__construct() |
| 11 | //~ { | 10 | //~ {} |
| 12 | //~ $this->table = strtolower(__CLASS__); | ||
| 13 | //~ echo "TABLE = " . $this->table . "\n"; | ||
| 14 | //~ } | ||
| 15 | 11 | ||
| 16 | // getters | 12 | // getters |
| 17 | public function getTable(): string | 13 | public function getTable(): string |
| @@ -281,25 +277,28 @@ abstract class Model extends DB | |||
| 281 | } | 277 | } |
| 282 | 278 | ||
| 283 | 279 | ||
| 284 | // fonction appelée une seule fois au lancement du programme | ||
| 285 | // le tableau nécessaire n'est pas copié en mémoire à l'instanciation (pas de fuite de mémoire), mais uniquement à l'appel de cette fonction statique, à la fin de la fonction la mémoire est libérée | ||
| 286 | // DBStructure::${self::$tableStructure} permet de nommer une variable statique de classe | ||
| 287 | static public function createTables() | 280 | static public function createTables() |
| 288 | { | 281 | { |
| 289 | foreach(StructTablesDB::$structureOfTables as $tableName => $oneTable) | 282 | static $first_time = true; |
| 283 | |||
| 284 | if($first_time) // fonction normallement appelée qu'une seule fois | ||
| 290 | { | 285 | { |
| 291 | //var_dump(StructTablesDB::${self::$tableStructure}); => propriété statique de classe dans une variable | 286 | foreach(StructTablesDB::$structureOfTables as $tableName => $oneTable) |
| 292 | $fields_and_types = []; | ||
| 293 | $query = 'CREATE TABLE IF NOT EXISTS ' . $tableName . ' ('; | ||
| 294 | foreach($oneTable as $key => $value) | ||
| 295 | { | 287 | { |
| 296 | $fields_and_types[] = $key . ' ' . $value; | 288 | $query = 'CREATE TABLE IF NOT EXISTS ' . $tableName . ' ('; |
| 289 | foreach($oneTable as $key => $value) | ||
| 290 | { | ||
| 291 | $query .= $key . ' ' . $value . ', '; | ||
| 292 | } | ||
| 293 | $query .= 'PRIMARY KEY(ID AUTOINCREMENT));'; | ||
| 294 | parent::getInstance()->exec($query); | ||
| 297 | } | 295 | } |
| 298 | $query .= implode(', ', $fields_and_types); // implode() convertit un tableau en une chaîne avec un séparateur entre chaque élément | ||
| 299 | $query .= ', PRIMARY KEY(ID AUTOINCREMENT));'; | ||
| 300 | //echo($query . "\n\n"); | ||
| 301 | 296 | ||
| 302 | parent::getInstance()->exec($query); // merci singleton! | 297 | $first_time = false; |
| 298 | } | ||
| 299 | else | ||
| 300 | { | ||
| 301 | echo "Model::createTables() a déjà été appelée et ne fera rien.\n"; | ||
| 303 | } | 302 | } |
| 304 | } | 303 | } |
| 305 | } | 304 | } |
