From 2ce0130b7d88fa5083f1cf4f8997035209001835 Mon Sep 17 00:00:00 2001 From: Krivchikov Dmitry Date: Thu, 3 Apr 2014 18:59:47 +0400 Subject: [PATCH] =?UTF-8?q?FIX=20=D0=BE=D0=BF=D0=B5=D1=87=D0=B0=D1=82?= =?UTF-8?q?=D0=BA=D0=B0=20+=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8F?= =?UTF-8?q?=20save=20=D0=BF=D1=80=D0=B8=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B5=20=D1=81=20=D0=BC=D0=BE=D0=B4=D0=B5=D0=BB=D1=8C=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ground/app.php | 2 +- ground/classes/Model.php | 21 +++++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/ground/app.php b/ground/app.php index 966be53..8b18331 100644 --- a/ground/app.php +++ b/ground/app.php @@ -4,7 +4,7 @@ function __autoload($className) { $dirs = array('classes','models','controllers'); - $loaded = flase; + $loaded = false; foreach ($dirs as $dir) { $fileName = dirname(__FILE__) . '/../ground/' . $dir. '/' . $className . '.php'; diff --git a/ground/classes/Model.php b/ground/classes/Model.php index fb3ea48..b9e8a70 100644 --- a/ground/classes/Model.php +++ b/ground/classes/Model.php @@ -23,7 +23,7 @@ class Model return isset($this->_vals_[$name]) ? $this->_vals_[$name] : null; } - function getPk() + private function getPk() { $res = App::$DB->query("SHOW KEYS FROM " . $this->_tableName_ . " WHERE Key_name = 'PRIMARY'")->fetch(PDO::FETCH_ASSOC); return $res["Column_name"]; @@ -31,13 +31,16 @@ class Model function getByPK($id) { - return App::$DB->query('SELECT * FROM ' . $this->_tableName_ . ' WHERE ' . $this->_primaryKey_ . ' = ' . $id)->fetch(PDO::FETCH_ASSOC); + $this->_vals_ = App::$DB->query('SELECT * FROM ' . $this->_tableName_ . ' WHERE ' . $this->_primaryKey_ . ' = ' . $id)->fetch(PDO::FETCH_ASSOC); + $this->_id_ = $this->_vals_[$this->_primaryKey_]; + return $this; } function getAll() { $res = App::$DB->query('SELECT * FROM ' . $this->_tableName_); + $this->_id_ = NULL; // Это больше не конкретная запись $ready = array(); while ($row = $res->fetch(PDO::FETCH_ASSOC)) @@ -60,4 +63,18 @@ class Model return $request->execute((array) $values); } + function save() + { + if (!$this->_id_ || !$this->_vals_) + return FALSE; + + $values = $this->_vals_; + unset($values[$this->_primaryKey_]); + + $request = App::$DB->prepare('UPDATE ' . $this->_tableName_ . ' SET ' . implode('=?, ', array_keys($values)) . '=? WHERE '.$this->_primaryKey_.'='.$this->_id_); + $res = $request->execute(array_values($values)); + + return $res ? $this : FALSE; + } + }