|
|
@ -1,4 +1,5 @@ |
|
|
|
<?php |
|
|
|
|
|
|
|
/** |
|
|
|
* Класс модели данных |
|
|
|
* |
|
|
@ -13,35 +14,35 @@ |
|
|
|
|
|
|
|
abstract class Model |
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* DbDriver instance |
|
|
|
* |
|
|
|
* |
|
|
|
* @var DbDriver |
|
|
|
*/ |
|
|
|
protected $db; |
|
|
|
|
|
|
|
|
|
|
|
protected $cache; |
|
|
|
|
|
|
|
|
|
|
|
protected $table; |
|
|
|
|
|
|
|
|
|
|
|
protected $connection = 'default'; |
|
|
|
|
|
|
|
|
|
|
|
protected $key = 'id'; |
|
|
|
|
|
|
|
|
|
|
|
public function __construct() |
|
|
|
{ |
|
|
|
$this->db = Db::connect($this->connection); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* @return int |
|
|
|
*/ |
|
|
|
public function getInsertId() |
|
|
|
{ |
|
|
|
return $this->db->getInsertId($this->table(), $this->key); |
|
|
|
return $this->db->getInsertId($this->table(false), $this->key); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* @param string $ident |
|
|
|
* @return string Quoted identifier. |
|
|
@ -50,7 +51,7 @@ abstract class Model |
|
|
|
{ |
|
|
|
return $this->db->quoteIdentifier($ident); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* @param mixed $value |
|
|
|
* @return string Quoted value. |
|
|
@ -59,18 +60,17 @@ abstract class Model |
|
|
|
{ |
|
|
|
return $this->db->quote($value); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* @param int $id |
|
|
|
* @return object |
|
|
|
*/ |
|
|
|
public function get($id) |
|
|
|
{ |
|
|
|
$sql = 'SELECT * FROM ' . $this->identify($this->table()) |
|
|
|
. ' WHERE ' . $this->identify($this->key) . '=' . (int) $id; |
|
|
|
$sql = 'SELECT * FROM ' . $this->table() . ' WHERE ' . $this->identify($this->key) . '=' . (int) $id; |
|
|
|
return $this->db->query($sql)->fetch(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* @param array $data |
|
|
|
* @return int Affect row or id of inserted field. |
|
|
@ -87,19 +87,19 @@ abstract class Model |
|
|
|
} |
|
|
|
return $result; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* @param array $data |
|
|
|
* @return int Id of inserted row |
|
|
|
*/ |
|
|
|
public function insert($data) |
|
|
|
{ |
|
|
|
if (! $this->db->insert($this->table(), $data)) { |
|
|
|
if (!$this->db->insert($this->table(false), $data)) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
return $this->getInsertId(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* @param array $data |
|
|
|
* @param mixed $where |
|
|
@ -107,9 +107,9 @@ abstract class Model |
|
|
|
*/ |
|
|
|
public function update($data, $where) |
|
|
|
{ |
|
|
|
return $this->db->update($this->table(), $data, $where); |
|
|
|
return $this->db->update($this->table(false), $data, $where); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* @param mixed $where |
|
|
|
* @return int Number of affected rows |
|
|
@ -119,17 +119,18 @@ abstract class Model |
|
|
|
if (is_int($where)) { |
|
|
|
$where = $this->identify($this->key) . '=' . (int) $where; |
|
|
|
} |
|
|
|
return $this->db->delete($this->table(), $where); |
|
|
|
return $this->db->delete($this->table(false), $where); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* @param bool $autoescape |
|
|
|
* @return string |
|
|
|
*/ |
|
|
|
public function table() |
|
|
|
public function table($autoescape = true) |
|
|
|
{ |
|
|
|
if (! $this->table) { |
|
|
|
if (!$this->table) { |
|
|
|
$this->table = substr(strtolower(get_class($this)), 0, -5/*strlen('Model')*/); |
|
|
|
} |
|
|
|
return $this->table; |
|
|
|
return $identify ? $this->identify($this->table) : $this->table; |
|
|
|
} |
|
|
|
} |