1
0
Fork 0

Optimized Models queries

This commit is contained in:
p1907961 2020-12-20 13:57:08 +01:00
parent 86dc860290
commit 303075c495
2 changed files with 29 additions and 22 deletions

View file

@ -1,20 +1,25 @@
<?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(){
try { if(!Model::$db){
/* try {
* Init connection to the DB /*
*/ * Init connection to the DB
Model::$db = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASSWORD); */
Model::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); Model::$db = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASSWORD);
} catch(Exception $e){ Model::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
var_dump($e); } catch(Exception $e){
var_dump($e);
}
} }
} }
/* /*
@ -52,14 +57,18 @@ class Model {
} }
protected function getColumns(): array protected function getColumns(): array
{ {
$q = Model::$db->prepare('SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ?'); if(!isset(get_called_class()::$column[get_called_class()])) {
$q->execute(array(get_called_class())); get_called_class()::$column[get_called_class()] = [];
$d = $q->fetchAll(PDO::FETCH_ASSOC); $q = Model::$db->prepare('SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ?');
$out = []; $q->execute(array(get_called_class()));
forEach($d as $col){ $d = $q->fetchAll(PDO::FETCH_ASSOC);
$out[$col['COLUMN_NAME']] = $col; $out = [];
foreach ($d as $col) {
$out[$col['COLUMN_NAME']] = $col;
}
get_called_class()::$column[get_called_class()] = $out;
} }
return $out; return get_called_class()::$column[get_called_class()];
} }
} }
/* /*

View file

@ -37,13 +37,11 @@ require_once('template/head.php');
</div> </div>
<div class="form-group"> <div class="form-group">
<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>