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.

280 lines
6.7 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. $affected = $this->db->insert($this->table(false), $data, $on_duplicate);
  86. return ($this->getInsertId()) ? $this->getInsertId() : $affected;
  87. }
  88. /**
  89. * @param array $data
  90. * @param mixed $where
  91. * @return int Number of affected rows
  92. */
  93. public function update($data, $where)
  94. {
  95. if (is_int($where)) {
  96. $where = $this->identify($this->key) . '=' . (int) $where;
  97. }
  98. return $this->db->update($this->table(false), $data, $where);
  99. }
  100. /**
  101. * @param int|array $where Int or array ids
  102. * @return int Number of affected rows
  103. */
  104. public function delete($where)
  105. {
  106. if (is_array($where)) {
  107. $where = $this->identify($this->key) . ' IN (' . $this->quote($where) . ')';
  108. } else {
  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. * Creates order sql string
  126. *
  127. * @param array $params
  128. * @param array $sortable
  129. * @return string
  130. */
  131. protected function order($params, $sortable = array('id'))
  132. {
  133. $sql = '';
  134. if (isset($params['sort'])) {
  135. $order = (isset($params['order']) && $params['order'] == 'desc') ? 'DESC' : 'ASC';
  136. if (in_array($params['sort'], $sortable)) {
  137. $sql = ' ORDER BY ' . $this->identify($params['sort']) . ' ' . $order;
  138. }
  139. }
  140. return $sql;
  141. }
  142. /**
  143. * Searches using like
  144. *
  145. * @param array $params
  146. * @param array $searchable
  147. * @param string $table_prefix
  148. * @return string
  149. */
  150. protected function search($params, $searchable = array('id'), $table_prefix = '')
  151. {
  152. $sql = '';
  153. if (isset($params['q']) && isset($params['qt']) && in_array($params['qt'], $searchable)) {
  154. if ($table_prefix) {
  155. $sql = $table_prefix . '.';
  156. }
  157. $sql .= $this->identify($params['qt']) . ' LIKE ' . $this->quote('%' . $params['q'] . '%');
  158. }
  159. return $sql;
  160. }
  161. /**
  162. * @return Cache
  163. */
  164. public function getCache()
  165. {
  166. if (!$this->cache) {
  167. $this->cache = Cacher::get(Config::get(__CLASS__, 'MemcacheCache'));
  168. }
  169. return $this->cache;
  170. }
  171. /**
  172. * @param string $key
  173. * @return int
  174. */
  175. public function getKeyExpire($key)
  176. {
  177. return (isset($this->cache_keys[$key])) ? ($this->cache_keys[$key] * 60) : 0;
  178. }
  179. /**
  180. * @param string $name
  181. * @param array $params
  182. * @return CacheKey
  183. */
  184. protected function cacheKey($name, $params = array())
  185. {
  186. if (substr(strtolower($name), -3, 3) == 'set') {
  187. return new CacheKeySet($name, $params, $this);
  188. }
  189. return new CacheKey($name, $params, $this);
  190. }
  191. /**
  192. * @param CacheKey | CacheKeySet $cache
  193. */
  194. protected function addCleanCache($cache)
  195. {
  196. $this->caches_clean[] = $cache;
  197. }
  198. protected function cleanCaches()
  199. {
  200. // cleaning caches
  201. foreach ($this->caches_clean as $cache) {
  202. $cache->del();
  203. }
  204. $this->caches_clean = array();
  205. }
  206. /**
  207. * @param string $sql
  208. * @param array $params
  209. * @param string $field
  210. * @param CacheKey | CacheKeySet $cache_key
  211. */
  212. protected function fetchField($sql, $params = array(), $field, $cache_key = null)
  213. {
  214. if (!$cache_key || !$result = $cache_key->get()) {
  215. $result = $this->db->query($sql, $params)->fetchField($field);
  216. if ($cache_key) {
  217. $cache_key->set($result);
  218. }
  219. }
  220. return $result;
  221. }
  222. /**
  223. * @param string $sql
  224. * @param array $params
  225. * @param CacheKey | CacheKeySet $cache_key
  226. */
  227. protected function fetch($sql, $params = array(), $cache_key = null)
  228. {
  229. if (!$cache_key || !$result = $cache_key->get()) {
  230. $result = $this->db->query($sql, $params)->fetch();
  231. if ($cache_key) {
  232. $cache_key->set($result);
  233. }
  234. }
  235. return $result;
  236. }
  237. /**
  238. * @param string $sql
  239. * @param array $params
  240. * @param CacheKey | CacheKeySet $cache_key
  241. */
  242. protected function fetchAll($sql, $params = array(), $cache_key = null)
  243. {
  244. if (!$cache_key || !$result = $cache_key->get()) {
  245. $result = $this->db->query($sql, $params)->fetchAll();
  246. if ($cache_key) {
  247. $cache_key->set($result);
  248. }
  249. }
  250. return $result;
  251. }
  252. }