diff options
author | polo <ordipolo@gmx.fr> | 2022-12-20 03:31:33 +0100 |
---|---|---|
committer | polo <ordipolo@gmx.fr> | 2022-12-20 03:31:33 +0100 |
commit | 7d564efbccc4b361d2fa2db2902fb35882304aae (patch) | |
tree | efc28d94a1387f918c84201318880d74a52adf84 /src/model | |
parent | dedbe2f5cee33431c1299c7f0dbef4e247dc2447 (diff) | |
download | AppliGestionPHP-7d564efbccc4b361d2fa2db2902fb35882304aae.zip |
recherche de clients
Diffstat (limited to 'src/model')
-rw-r--r-- | src/model/Clients.php | 32 | ||||
-rw-r--r-- | src/model/Model.php | 16 |
2 files changed, 40 insertions, 8 deletions
diff --git a/src/model/Clients.php b/src/model/Clients.php index 816cff3..92a4b31 100644 --- a/src/model/Clients.php +++ b/src/model/Clients.php | |||
@@ -46,10 +46,36 @@ class Clients extends Model | |||
46 | } | 46 | } |
47 | 47 | ||
48 | 48 | ||
49 | public function newRow(array $entry) | 49 | public function newRow(array $input) |
50 | { | 50 | { |
51 | $this->hydrate(['prenom_nom' => $entry[0], 'adresse' => $entry[1], 'code_client' => $entry[2], 'commentaires' => $entry[3]]); | 51 | $this->hydrate(['prenom_nom' => $input[0], 'adresse' => $input[1], 'code_client' => $input[2], 'commentaires' => $input[3]]); |
52 | $this->create(); | 52 | $this->create(); |
53 | $this->setIdFromLastInsertID(); | 53 | $this->setIdFromLastInsertID(); // dans ModelChildren, n'utilise pas Model::execQuery() |
54 | } | ||
55 | |||
56 | public function findByKeywords(array $keywords, string $field): array | ||
57 | { | ||
58 | $result = []; | ||
59 | for($i = 0; $i < count($keywords); $i++) | ||
60 | { | ||
61 | // tableau à deux dimensions obtenu pour un mot clé | ||
62 | $query_result = $this->execQuery('SELECT * FROM ' . $this->table . ' WHERE ' . $field . ' LIKE "%' . $keywords[$i] . '%"')->fetchAll(); | ||
63 | foreach($query_result as $one_array) // pour chaque sous tableau | ||
64 | { | ||
65 | $already_exist = false; | ||
66 | for($j = 0; $j < count($result); $j++) // pour chaque tableau déjà enregistré dans le tableau $result | ||
67 | { | ||
68 | if($result[$j]['ID'] === $one_array['ID']) | ||
69 | { | ||
70 | $already_exist = true; | ||
71 | } | ||
72 | } | ||
73 | if(!$already_exist) | ||
74 | { | ||
75 | $result[] = $one_array; | ||
76 | } | ||
77 | } | ||
78 | } | ||
79 | return($result); | ||
54 | } | 80 | } |
55 | } | 81 | } |
diff --git a/src/model/Model.php b/src/model/Model.php index 9effbe7..8cbf056 100644 --- a/src/model/Model.php +++ b/src/model/Model.php | |||
@@ -12,6 +12,12 @@ class Model extends DB | |||
12 | $this->db = parent::getInstance(); // connexion | 12 | $this->db = parent::getInstance(); // connexion |
13 | } | 13 | } |
14 | 14 | ||
15 | // getters | ||
16 | public function getTable(): string | ||
17 | { | ||
18 | return($this->table); | ||
19 | } | ||
20 | |||
15 | // setters (plusieurs en même temps) | 21 | // setters (plusieurs en même temps) |
16 | public function hydrate(array $data) // $data = tableau associatif en entrée: nom_du_champ => valeur | 22 | public function hydrate(array $data) // $data = tableau associatif en entrée: nom_du_champ => valeur |
17 | { | 23 | { |
@@ -78,17 +84,17 @@ class Model extends DB | |||
78 | 84 | ||
79 | 85 | ||
80 | // read SELECT | 86 | // read SELECT |
81 | public function readAll() // obtenir une table | 87 | public function readAll(): array // obtenir une table |
82 | { | 88 | { |
83 | $query = $this->execQuery('SELECT * FROM ' . $this->table . ';'); // fonctionne aussi sans le point virgule dans le SQL!! | 89 | return($this->execQuery('SELECT * FROM ' . $this->table)->fetchAll()); // fonctionne aussi sans le point virgule dans le SQL!! |
84 | return($query->fetchAll()); | ||
85 | } | 90 | } |
91 | |||
86 | public function findById(int $id) // obtenir une entrée avec son ID | 92 | public function findById(int $id) // obtenir une entrée avec son ID |
87 | { | 93 | { |
88 | return($this->execQuery('SELECT * FROM ' . $this->table . ' WHERE id = ' . $id)->fetch()); | 94 | return($this->execQuery('SELECT * FROM ' . $this->table . ' WHERE id = ' . $id)->fetch()); |
89 | } | 95 | } |
90 | 96 | ||
91 | public function find(array $criteria) // obtenir une entrée avec un tableau associatif 'champ' => 'valeur' | 97 | public function find(array $criteria): array // obtenir une entrée avec un tableau associatif 'champ' => 'valeur' |
92 | { | 98 | { |
93 | $fields = []; | 99 | $fields = []; |
94 | $values = []; | 100 | $values = []; |
@@ -134,7 +140,7 @@ class Model extends DB | |||
134 | } | 140 | } |
135 | 141 | ||
136 | 142 | ||
137 | // fonction appelée une seule fois pour chaque table | 143 | // fonction appelée une seule fois au lancement du programme |
138 | // 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 | 144 | // 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 |
139 | // DBStructure::${self::$tableStructure} permet de nommer une variable statique de classe | 145 | // DBStructure::${self::$tableStructure} permet de nommer une variable statique de classe |
140 | static public function createTables() | 146 | static public function createTables() |