FIX опечатка + функция save при работе с моделью

This commit is contained in:
Krivchikov Dmitry 2014-04-03 18:59:47 +04:00
parent d2dd7b9dd0
commit 2ce0130b7d
2 changed files with 20 additions and 3 deletions

View File

@ -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';

View File

@ -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;
}
}