Optimized Models queries
This commit is contained in:
parent
86dc860290
commit
303075c495
2 changed files with 29 additions and 22 deletions
|
@ -1,12 +1,16 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
class Model {
|
class Model {
|
||||||
|
|
||||||
protected static $db = null;
|
protected static $db = null;
|
||||||
|
private static $column;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->initDatabase();
|
$this->initDatabase();
|
||||||
}
|
}
|
||||||
public static function initDatabase(){
|
public static function initDatabase(){
|
||||||
|
if(!Model::$db){
|
||||||
try {
|
try {
|
||||||
/*
|
/*
|
||||||
* Init connection to the DB
|
* Init connection to the DB
|
||||||
|
@ -17,6 +21,7 @@ class Model {
|
||||||
var_dump($e);
|
var_dump($e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
/*
|
/*
|
||||||
* GET/SELECT Query
|
* GET/SELECT Query
|
||||||
*/
|
*/
|
||||||
|
@ -52,14 +57,18 @@ class Model {
|
||||||
}
|
}
|
||||||
protected function getColumns(): array
|
protected function getColumns(): array
|
||||||
{
|
{
|
||||||
|
if(!isset(get_called_class()::$column[get_called_class()])) {
|
||||||
|
get_called_class()::$column[get_called_class()] = [];
|
||||||
$q = Model::$db->prepare('SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ?');
|
$q = Model::$db->prepare('SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ?');
|
||||||
$q->execute(array(get_called_class()));
|
$q->execute(array(get_called_class()));
|
||||||
$d = $q->fetchAll(PDO::FETCH_ASSOC);
|
$d = $q->fetchAll(PDO::FETCH_ASSOC);
|
||||||
$out = [];
|
$out = [];
|
||||||
forEach($d as $col){
|
foreach ($d as $col) {
|
||||||
$out[$col['COLUMN_NAME']] = $col;
|
$out[$col['COLUMN_NAME']] = $col;
|
||||||
}
|
}
|
||||||
return $out;
|
get_called_class()::$column[get_called_class()] = $out;
|
||||||
|
}
|
||||||
|
return get_called_class()::$column[get_called_class()];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -39,11 +39,9 @@ require_once('template/head.php');
|
||||||
<label for="typeSelect">Quels services proposez-vous ?</label><br>
|
<label for="typeSelect">Quels services proposez-vous ?</label><br>
|
||||||
<?php
|
<?php
|
||||||
$c = 0;
|
$c = 0;
|
||||||
|
|
||||||
foreach ($services as $service){
|
foreach ($services as $service){
|
||||||
$c++;
|
$c++;
|
||||||
?>
|
?><div class="form-check form-check-inline">
|
||||||
<div class="form-check form-check-inline">
|
|
||||||
<input class="form-check-input" type="checkbox" id="inlineCheckbox<?=$c?>" value="<?=htmlspecialchars($service->getName())?>">
|
<input class="form-check-input" type="checkbox" id="inlineCheckbox<?=$c?>" value="<?=htmlspecialchars($service->getName())?>">
|
||||||
<label class="form-check-label" for="inlineCheckbox<?=$c?>"><?=htmlspecialchars($service->getName())?></label>
|
<label class="form-check-label" for="inlineCheckbox<?=$c?>"><?=htmlspecialchars($service->getName())?></label>
|
||||||
</div>
|
</div>
|
||||||
|
|
Reference in a new issue