You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

242 lines
5.6 KiB

  1. <?php
  2. /**
  3. * Класс модели данных
  4. *
  5. * @copyright NetMonsters <team@netmonsters.ru>
  6. * @link http://netmonsters.ru
  7. * @package Majestic
  8. * @subpackage Model
  9. * @since 2010-02-16
  10. * @version SVN: $Id$
  11. * @filesource $URL$
  12. */
  13. abstract class Model implements iCacheable
  14. {
  15. /**
  16. * DbDriver instance
  17. *
  18. * @var DbDriver
  19. */
  20. protected $db;
  21. /**
  22. * Cache instance
  23. *
  24. * @var Cache
  25. */
  26. protected $cache;
  27. /**
  28. * Custom expiration time for keys
  29. *
  30. * @var mixed
  31. */
  32. protected $cache_keys = array();
  33. /**
  34. * Caches to clean.
  35. *
  36. * @var mixed
  37. */
  38. protected $caches_clean = array();
  39. protected $table;
  40. protected $connection = 'default';
  41. protected $key = 'id';
  42. public function __construct()
  43. {
  44. $this->db = Db::connect($this->connection);
  45. }
  46. /**
  47. * @return int
  48. */
  49. public function getInsertId()
  50. {
  51. return $this->db->getInsertId($this->table(false), $this->key);
  52. }
  53. /**
  54. * @param string $ident
  55. * @return string Quoted identifier.
  56. */
  57. public function identify($ident)
  58. {
  59. return $this->db->quoteIdentifier($ident);
  60. }
  61. /**
  62. * @param mixed $value
  63. * @return string Quoted value.
  64. */
  65. public function quote($value)
  66. {
  67. return $this->db->quote($value);
  68. }
  69. /**
  70. * @param int $id
  71. * @return object
  72. */
  73. public function get($id)
  74. {
  75. $sql = 'SELECT * FROM ' . $this->table() . ' WHERE ' . $this->identify($this->key) . '=?';
  76. return $this->fetch($sql, $id);
  77. }
  78. /**
  79. * @param array $data
  80. * @param array $on_duplicate
  81. * @return int Id of inserted row
  82. */
  83. public function insert($data, $on_duplicate = array())
  84. {
  85. if (!$affected = $this->db->insert($this->table(false), $data, $on_duplicate)) {
  86. return false;
  87. }
  88. return ($this->getInsertId()) ? $this->getInsertId() : $affected;
  89. }
  90. /**
  91. * @param array $data
  92. * @param mixed $where
  93. * @return int Number of affected rows
  94. */
  95. public function update($data, $where)
  96. {
  97. if (is_int($where)) {
  98. $where = $this->identify($this->key) . '=' . (int) $where;
  99. }
  100. return $this->db->update($this->table(false), $data, $where);
  101. }
  102. /**
  103. * @param int|array $where Int or array ids
  104. * @return int Number of affected rows
  105. */
  106. public function delete($where)
  107. {
  108. if (is_array($where)) {
  109. $where = $this->identify($this->key) . ' IN (' . $this->quote($where) . ')';
  110. } else {
  111. $where = $this->identify($this->key) . '=' . (int) $where;
  112. }
  113. return $this->db->delete($this->table(false), $where);
  114. }
  115. /**
  116. * @param bool $autoindent
  117. * @return string
  118. */
  119. protected function table($autoindent = true)
  120. {
  121. if (!$this->table) {
  122. $this->table = substr(strtolower(get_class($this)), 0, -5/*strlen('Model')*/);
  123. }
  124. return $autoindent ? $this->identify($this->table) : $this->table;
  125. }
  126. /**
  127. * @return Cache
  128. */
  129. public function getCache()
  130. {
  131. if (!$this->cache) {
  132. $this->cache = Cacher::get(Config::get(__CLASS__, 'MemcacheCache'));
  133. }
  134. return $this->cache;
  135. }
  136. /**
  137. * @param string $key
  138. * @return int
  139. */
  140. public function getKeyExpire($key)
  141. {
  142. return (isset($this->cache_keys[$key])) ? ($this->cache_keys[$key] * 60) : 0;
  143. }
  144. /**
  145. * @param string $name
  146. * @param array $params
  147. * @return CacheKey
  148. */
  149. protected function cacheKey($name, $params = array())
  150. {
  151. if (substr(strtolower($name), -3, 3) == 'set') {
  152. return new CacheKeySet($name, $params, $this);
  153. }
  154. return new CacheKey($name, $params, $this);
  155. }
  156. /**
  157. * @param CacheKey | CacheKeySet $cache
  158. */
  159. protected function addCleanCache($cache)
  160. {
  161. $this->caches_clean[] = $cache;
  162. }
  163. protected function cleanCaches()
  164. {
  165. // cleaning caches
  166. foreach ($this->caches_clean as $cache) {
  167. $cache->del();
  168. }
  169. $this->caches_clean = array();
  170. }
  171. /**
  172. * @param string $sql
  173. * @param array $params
  174. * @param string $field
  175. * @param CacheKey | CacheKeySet $cache_key
  176. */
  177. protected function fetchField($sql, $params = array(), $field, $cache_key = null)
  178. {
  179. if (!$cache_key || !$result = $cache_key->get()) {
  180. $result = $this->db->query($sql, $params)->fetchField($field);
  181. if ($cache_key) {
  182. $cache_key->set($result);
  183. }
  184. }
  185. return $result;
  186. }
  187. /**
  188. * @param string $sql
  189. * @param array $params
  190. * @param CacheKey | CacheKeySet $cache_key
  191. */
  192. protected function fetch($sql, $params = array(), $cache_key = null)
  193. {
  194. if (!$cache_key || !$result = $cache_key->get()) {
  195. $result = $this->db->query($sql, $params)->fetch();
  196. if ($cache_key) {
  197. $cache_key->set($result);
  198. }
  199. }
  200. return $result;
  201. }
  202. /**
  203. * @param string $sql
  204. * @param array $params
  205. * @param CacheKey | CacheKeySet $cache_key
  206. */
  207. protected function fetchAll($sql, $params = array(), $cache_key = null)
  208. {
  209. if (!$cache_key || !$result = $cache_key->get()) {
  210. $result = $this->db->query($sql, $params)->fetchAll();
  211. if ($cache_key) {
  212. $cache_key->set($result);
  213. }
  214. }
  215. return $result;
  216. }
  217. }