64 lines
1.3 KiB
PHP
64 lines
1.3 KiB
PHP
<?php
|
|
|
|
class Model
|
|
{
|
|
|
|
private $_primaryKey_;
|
|
protected $_tableName_;
|
|
private $_id_;
|
|
private $_vals_ = array();
|
|
|
|
function __construct()
|
|
{
|
|
$this->_primaryKey_ = $this->getPk();
|
|
}
|
|
|
|
function __set($name, $value)
|
|
{
|
|
$this->_vals_[$name] = $value;
|
|
}
|
|
|
|
function __get($name)
|
|
{
|
|
return isset($this->_vals_[$name]) ? $this->_vals_[$name] : null;
|
|
}
|
|
|
|
function getPk()
|
|
{
|
|
$res = App::$DB->query("SHOW KEYS FROM " . $this->_tableName_ . " WHERE Key_name = 'PRIMARY'")->fetch(PDO::FETCH_ASSOC);
|
|
return $res["Column_name"];
|
|
}
|
|
|
|
function getByPK($id)
|
|
{
|
|
return App::$DB->query('SELECT * FROM ' . $this->_tableName_ . ' WHERE ' . $this->_primaryKey_ . ' = ' . $id)->fetch(PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
function getAll()
|
|
{
|
|
$res = App::$DB->query('SELECT * FROM ' . $this->_tableName_);
|
|
|
|
$ready = array();
|
|
|
|
while ($row = $res->fetch(PDO::FETCH_ASSOC))
|
|
{
|
|
$ready[$row[$this->_primaryKey_]] = $row;
|
|
}
|
|
|
|
return $ready;
|
|
}
|
|
|
|
function delete($id)
|
|
{
|
|
return App::$DB->query('DELETE FROM ' . $this->_tableName_ . ' WHERE ' . $this->_primaryKey_ . ' = ' . $id);
|
|
}
|
|
|
|
function add($values)
|
|
{
|
|
|
|
$request = App::$DB->prepare('INSERT INTO ' . $this->_tableName_ . ' (' . implode(',', array_keys($values)) . ') values (:' . implode(', :', array_keys($values)) . ')');
|
|
return $request->execute((array) $values);
|
|
}
|
|
|
|
}
|