From 303075c4959f6252f6dc42296c6cc504988c9e35 Mon Sep 17 00:00:00 2001 From: p1907961 Date: Sun, 20 Dec 2020 13:57:08 +0100 Subject: [PATCH] Optimized Models queries --- src/model.php | 39 ++++++++++++++++++++++++--------------- view/add_accomodation.php | 12 +++++------- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/src/model.php b/src/model.php index 06a8765..c72c426 100644 --- a/src/model.php +++ b/src/model.php @@ -1,20 +1,25 @@ 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()]; } } /* diff --git a/view/add_accomodation.php b/view/add_accomodation.php index de5efae..07aced6 100644 --- a/view/add_accomodation.php +++ b/view/add_accomodation.php @@ -37,13 +37,11 @@ require_once('template/head.php');

- -
+