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
class Model {
protected static $db = null;
private static $column;
public function __construct()
{
$this->initDatabase();
}
public static function initDatabase(){
try {
/*
* 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);
} catch(Exception $e){
var_dump($e);
if(!Model::$db){
try {
/*
* 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);
} catch(Exception $e){
var_dump($e);
}
}
}
/*
@ -52,14 +57,18 @@ class Model {
}
protected function getColumns(): array
{
$q = Model::$db->prepare('SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ?');
$q->execute(array(get_called_class()));
$d = $q->fetchAll(PDO::FETCH_ASSOC);
$out = [];
forEach($d as $col){
$out[$col['COLUMN_NAME']] = $col;
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->execute(array(get_called_class()));
$d = $q->fetchAll(PDO::FETCH_ASSOC);
$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 class="form-group">
<label for="typeSelect">Quels services proposez-vous ?</label><br>
<?php
$c = 0;
foreach ($services as $service){
$c++;
?>
<div class="form-check form-check-inline">
<?php
$c = 0;
foreach ($services as $service){
$c++;
?><div class="form-check form-check-inline">
<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>
</div>