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.

240 lines
5.4 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 (!$res = $this->db->insert($this->table(false), $data, $on_duplicate)) {
  86. return false;
  87. }
  88. return ($on_duplicate) ? $res : $this->getInsertId();
  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 mixed $where
  104. * @return int Number of affected rows
  105. */
  106. public function delete($where)
  107. {
  108. if (is_int($where)) {
  109. $where = $this->identify($this->key) . '=' . (int) $where;
  110. }
  111. return $this->db->delete($this->table(false), $where);
  112. }
  113. /**
  114. * @param bool $autoindent
  115. * @return string
  116. */
  117. protected function table($autoindent = true)
  118. {
  119. if (!$this->table) {
  120. $this->table = substr(strtolower(get_class($this)), 0, -5/*strlen('Model')*/);
  121. }
  122. return $autoindent ? $this->identify($this->table) : $this->table;
  123. }
  124. /**
  125. * @return Cache
  126. */
  127. public function getCache()
  128. {
  129. if (!$this->cache) {
  130. $this->cache = Cacher::get(Config::get(__CLASS__, 'MemcacheCache'));
  131. }
  132. return $this->cache;
  133. }
  134. /**
  135. * @param string $key
  136. * @return int
  137. */
  138. public function getKeyExpire($key)
  139. {
  140. return (isset($this->cache_keys[$key])) ? ($this->cache_keys[$key] * 60) : 0;
  141. }
  142. /**
  143. * @param string $name
  144. * @param array $params
  145. * @return CacheKey
  146. */
  147. protected function cacheKey($name, $params = array())
  148. {
  149. if (substr(strtolower($name), -3, 3) == 'set') {
  150. return new CacheKeySet($name, $params, $this);
  151. }
  152. return new CacheKey($name, $params, $this);
  153. }
  154. /**
  155. * @param CacheKey | CacheKeySet $cache
  156. */
  157. protected function addCleanCache($cache)
  158. {
  159. $this->caches_clean[] = $cache;
  160. }
  161. protected function cleanCaches()
  162. {
  163. // cleaning caches
  164. foreach ($this->caches_clean as $cache) {
  165. $cache->del();
  166. }
  167. $this->caches_clean = array();
  168. }
  169. /**
  170. * @param string $sql
  171. * @param array $params
  172. * @param string $field
  173. * @param CacheKey | CacheKeySet $cache_key
  174. */
  175. protected function fetchField($sql, $params = array(), $field, $cache_key = null)
  176. {
  177. if (!$cache_key || !$result = $cache_key->get()) {
  178. $result = $this->db->query($sql, $params)->fetchField($field);
  179. if ($cache_key) {
  180. $cache_key->set($result);
  181. }
  182. }
  183. return $result;
  184. }
  185. /**
  186. * @param string $sql
  187. * @param array $params
  188. * @param CacheKey | CacheKeySet $cache_key
  189. */
  190. protected function fetch($sql, $params = array(), $cache_key = null)
  191. {
  192. if (!$cache_key || !$result = $cache_key->get()) {
  193. $result = $this->db->query($sql, $params)->fetch();
  194. if ($cache_key) {
  195. $cache_key->set($result);
  196. }
  197. }
  198. return $result;
  199. }
  200. /**
  201. * @param string $sql
  202. * @param array $params
  203. * @param CacheKey | CacheKeySet $cache_key
  204. */
  205. protected function fetchAll($sql, $params = array(), $cache_key = null)
  206. {
  207. if (!$cache_key || !$result = $cache_key->get()) {
  208. $result = $this->db->query($sql, $params)->fetchAll();
  209. if ($cache_key) {
  210. $cache_key->set($result);
  211. }
  212. }
  213. return $result;
  214. }
  215. }