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