FIX опечатка + функция save при работе с моделью
This commit is contained in:
parent
d2dd7b9dd0
commit
2ce0130b7d
|
|
@ -4,7 +4,7 @@ function __autoload($className)
|
||||||
{
|
{
|
||||||
$dirs = array('classes','models','controllers');
|
$dirs = array('classes','models','controllers');
|
||||||
|
|
||||||
$loaded = flase;
|
$loaded = false;
|
||||||
foreach ($dirs as $dir) {
|
foreach ($dirs as $dir) {
|
||||||
$fileName = dirname(__FILE__) . '/../ground/' . $dir. '/' . $className . '.php';
|
$fileName = dirname(__FILE__) . '/../ground/' . $dir. '/' . $className . '.php';
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ class Model
|
||||||
return isset($this->_vals_[$name]) ? $this->_vals_[$name] : null;
|
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);
|
$res = App::$DB->query("SHOW KEYS FROM " . $this->_tableName_ . " WHERE Key_name = 'PRIMARY'")->fetch(PDO::FETCH_ASSOC);
|
||||||
return $res["Column_name"];
|
return $res["Column_name"];
|
||||||
|
|
@ -31,13 +31,16 @@ class Model
|
||||||
|
|
||||||
function getByPK($id)
|
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()
|
function getAll()
|
||||||
{
|
{
|
||||||
$res = App::$DB->query('SELECT * FROM ' . $this->_tableName_);
|
$res = App::$DB->query('SELECT * FROM ' . $this->_tableName_);
|
||||||
|
|
||||||
|
$this->_id_ = NULL; // Это больше не конкретная запись
|
||||||
$ready = array();
|
$ready = array();
|
||||||
|
|
||||||
while ($row = $res->fetch(PDO::FETCH_ASSOC))
|
while ($row = $res->fetch(PDO::FETCH_ASSOC))
|
||||||
|
|
@ -60,4 +63,18 @@ class Model
|
||||||
return $request->execute((array) $values);
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue