diff options
Diffstat (limited to 'src/model/Model.php')
-rw-r--r-- | src/model/Model.php | 95 |
1 files changed, 89 insertions, 6 deletions
diff --git a/src/model/Model.php b/src/model/Model.php index 4c1fb4d..6a7907c 100644 --- a/src/model/Model.php +++ b/src/model/Model.php | |||
@@ -1,16 +1,16 @@ | |||
1 | <?php | 1 | <?php |
2 | // php/Model.php | 2 | // php/Model.php |
3 | 3 | ||
4 | class Model extends DB | 4 | 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; | 8 | //static protected $tableStructure; |
9 | 9 | ||
10 | public function __construct() | 10 | public function __construct() // à surcharger |
11 | { | 11 | { |
12 | //~ $this->db = parent::getInstance(); // connexion | ||
13 | //~ $this->table = strtolower(__CLASS__); | 12 | //~ $this->table = strtolower(__CLASS__); |
13 | //~ echo "TABLE = " . $this->table . "\n"; | ||
14 | } | 14 | } |
15 | 15 | ||
16 | // getters | 16 | // getters |
@@ -19,7 +19,25 @@ class Model extends DB | |||
19 | return($this->table); | 19 | return($this->table); |
20 | } | 20 | } |
21 | 21 | ||
22 | public function getAll(): array | ||
23 | { | ||
24 | return get_object_vars($this); // retourne les propriétés de l'objet | ||
25 | } | ||
26 | |||
22 | // setters | 27 | // setters |
28 | public function setID(int $value = 0) | ||
29 | { | ||
30 | if($value === 0) | ||
31 | { | ||
32 | $this->ID = $this->db->lastInsertId(); // méthode de PDO (attention ne gère pas la concurence) | ||
33 | } | ||
34 | else | ||
35 | { | ||
36 | $this->ID = $value; | ||
37 | } | ||
38 | return($this); | ||
39 | } | ||
40 | |||
23 | public function hydrate(array $data): bool // $data = tableau associatif en entrée: nom_du_champ => valeur | 41 | public function hydrate(array $data): bool // $data = tableau associatif en entrée: nom_du_champ => valeur |
24 | { | 42 | { |
25 | foreach($data as $key => $value) | 43 | foreach($data as $key => $value) |
@@ -41,6 +59,71 @@ class Model extends DB | |||
41 | return true; | 59 | return true; |
42 | } | 60 | } |
43 | 61 | ||
62 | // cette fonction reçoit des données d'un tableau simple, permettant d'associer des champs de formulaires aux noms différents des champs de la BDD | ||
63 | function hydrateFromForm(string $data_string, Object $Presta = NULL): bool // quand l'objet est $Details, on hydrate aussi $Presta | ||
64 | { | ||
65 | //~ $tableSize = count(StructTablesDB::$structureOfTables[$this->getTable()]); // int | ||
66 | |||
67 | if($data_string !== '') | ||
68 | { | ||
69 | $data_array = explode('|', $data_string); // array | ||
70 | $check = false; | ||
71 | switch($this->getTable()) | ||
72 | { | ||
73 | case 'clients'; | ||
74 | if($data_array[0] == '') | ||
75 | { | ||
76 | echo "debug: données insuffisantes, le nom du client doit au minimum être renseigné\n"; | ||
77 | return false; | ||
78 | } | ||
79 | else | ||
80 | { | ||
81 | $check = $this->hydrate(['prenom_nom' => $data_array[0], 'code_client' => $data_array[1], 'adresse' => $data_array[2], 'code_postal' => $data_array[3], 'ville' => $data_array[4], 'telephone' => $data_array[5], 'courriel' => $data_array[6], 'apropos' => $data_array[7]]); | ||
82 | } | ||
83 | break; | ||
84 | case 'prestations'; // inutilisé | ||
85 | break; | ||
86 | case 'devis'; | ||
87 | $check = $Presta->hydrate(['mode_paiement' => $data_array[6]]); | ||
88 | if($check) | ||
89 | { | ||
90 | $check = $this->hydrate(['taches' => $data_array[0], 'total_main_d_oeuvre' => $data_array[1], 'pieces' => $data_array[2], 'total_pieces' => $data_array[3], 'deplacement' => $data_array[4], 'total_HT' => $data_array[5], 'delai_livraison' => $data_array[7], 'validite_devis' => $data_array[8], 'signature_devis' => $data_array[9]]); | ||
91 | } | ||
92 | break; | ||
93 | case 'factures'; | ||
94 | $check = $Presta->hydrate(['mode_paiement' => $data_array[10]]); | ||
95 | if($check) | ||
96 | { | ||
97 | $check = $this->hydrate(['taches' => $data_array[0], 'machine' => $data_array[1], 'OS' => $data_array[2], 'donnees' => $data_array[3], 'cles_licences' => $data_array[4], 'total_main_d_oeuvre' => $data_array[5], 'pieces' => $data_array[6], 'total_pieces' => $data_array[7], 'deplacement' => $data_array[8], 'total_HT' => $data_array[9]]); | ||
98 | } | ||
99 | break; | ||
100 | case 'cesu'; | ||
101 | $check = $Presta->hydrate(['mode_paiement' => $data_array[3]]); | ||
102 | if($check) | ||
103 | { | ||
104 | $check = $this->hydrate(['taches' => $data_array[0], 'duree_travail' => $data_array[1], 'salaire' => $data_array[2]]); | ||
105 | } | ||
106 | break; | ||
107 | case 'locations'; | ||
108 | //~ $check = $Presta->hydrate(['mode_paiement' => $data_array[11]]); | ||
109 | //~ if($check) | ||
110 | //~ { | ||
111 | $check = $this->hydrate(['designation' => $data_array[0], 'modele_description' => $data_array[1], 'valeur' => $data_array[2], 'etat_des_lieux_debut' => $data_array[3], 'etat_des_lieux_fin' => $data_array[4], 'duree_location' => $data_array[5], 'loyer_mensuel' => $data_array[6], 'loyers_payes' => $data_array[7], 'caution' => $data_array[8]]); | ||
112 | //~ } | ||
113 | break; | ||
114 | default: // inutilisé | ||
115 | echo "debug: table inconnue hydrateFromForm()"; | ||
116 | return false; | ||
117 | } | ||
118 | return $check; | ||
119 | } | ||
120 | else | ||
121 | { | ||
122 | echo "debug: annulation lors du formulaire\n"; | ||
123 | return false; | ||
124 | } | ||
125 | } | ||
126 | |||
44 | 127 | ||
45 | // exécuter le SQL | 128 | // exécuter le SQL |
46 | // les attributs correspondent aux ? dans les requêtes préparées | 129 | // les attributs correspondent aux ? dans les requêtes préparées |
@@ -51,8 +134,8 @@ class Model extends DB | |||
51 | 134 | ||
52 | if($attributes !== null) // requête préparée | 135 | if($attributes !== null) // requête préparée |
53 | { | 136 | { |
54 | var_dump($sql); | 137 | //~ var_dump($sql); |
55 | var_dump($attributes); | 138 | //~ var_dump($attributes); |
56 | $query = $this->db->prepare($sql); | 139 | $query = $this->db->prepare($sql); |
57 | $query->execute($attributes); | 140 | $query->execute($attributes); |
58 | return $query; | 141 | return $query; |
@@ -75,7 +158,7 @@ class Model extends DB | |||
75 | //~ var_dump($this); | 158 | //~ var_dump($this); |
76 | foreach($this as $field => $value) | 159 | foreach($this as $field => $value) |
77 | { | 160 | { |
78 | var_dump($field); var_dump($value); | 161 | //~ var_dump($field); var_dump($value); |
79 | // champs non renseignées et variables de l'objet qui ne sont pas des champs | 162 | // champs non renseignées et variables de l'objet qui ne sont pas des champs |
80 | // note: avec le !== (au lieu de !=) une valeur 0 est différente de null | 163 | // note: avec le !== (au lieu de !=) une valeur 0 est différente de null |
81 | if($value !== null && $field != 'db' && $field != 'table') | 164 | if($value !== null && $field != 'db' && $field != 'table') |