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.

183 lines
3.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
  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 $key = 'id';
  41. public function __construct($connection = 'default')
  42. {
  43. $this->db = Db::connect($connection);
  44. }
  45. /**
  46. * @return int
  47. */
  48. public function getInsertId()
  49. {
  50. return $this->db->getInsertId($this->table(), $this->key);
  51. }
  52. /**
  53. * @param array $data
  54. * @param array $on_duplicate
  55. * @return int Id of inserted row
  56. */
  57. public function insert($data)
  58. {
  59. $affected = $this->db->insert($this->table(), $data);
  60. return ($this->getInsertId()) ? $this->getInsertId() : $affected;
  61. }
  62. /**
  63. * @param array $data
  64. * @param mixed $where
  65. * @return int Number of affected rows
  66. */
  67. public function update($data, $where)
  68. {
  69. return $this->db->update($this->table(), $data, $where);
  70. }
  71. /**
  72. * @return string
  73. */
  74. protected function table()
  75. {
  76. if (!$this->table) {
  77. $this->table = substr(strtolower(get_class($this)), 0, -5/*strlen('Model')*/);
  78. }
  79. return $this->table;
  80. }
  81. /* Cache workaround */
  82. /**
  83. * @return Cache
  84. */
  85. public function getCache()
  86. {
  87. if (!$this->cache) {
  88. $this->cache = Cacher::get(Config::get(__CLASS__, 'MemcacheCache'));
  89. }
  90. return $this->cache;
  91. }
  92. /**
  93. * @param string $name
  94. * @param array $params
  95. * @return CacheKey
  96. */
  97. protected function cacheKey($name, $params = array())
  98. {
  99. $expire = (isset($this->cache_keys[$name])) ? ($this->cache_keys[$name] * 60) : 0;
  100. return new CacheKey($this->getCache(), $name, $params, $expire);
  101. }
  102. /**
  103. * @param CacheKey $cache
  104. */
  105. protected function addCleanCache($cache)
  106. {
  107. $this->caches_clean[] = $cache;
  108. }
  109. protected function cleanCaches()
  110. {
  111. // cleaning caches
  112. foreach ($this->caches_clean as $cache) {
  113. $cache->del();
  114. }
  115. $this->caches_clean = array();
  116. }
  117. /**
  118. * Abstract methods
  119. */
  120. /**
  121. * @param int $id
  122. * @return object
  123. */
  124. abstract public function get($id);
  125. /**
  126. * @param int $id Int id
  127. * @return int Number of affected rows
  128. */
  129. abstract public function delete($id);
  130. /**
  131. * Creates order sql string
  132. *
  133. * @param array $params
  134. * @param array $sortable
  135. * @return string
  136. */
  137. abstract protected function order($params, $sortable = array('id'));
  138. /**
  139. * @param string $sql
  140. * @param array $params
  141. * @param string $field
  142. * @param CacheKey $cache_key
  143. */
  144. abstract protected function fetchField($data, $params = array(), $field, $cache_key = null);
  145. /**
  146. * @param string $sql
  147. * @param array $params
  148. * @param CacheKey $cache_key
  149. */
  150. abstract protected function fetch($data, $params = array(), $cache_key = null);
  151. /**
  152. * @param string $sql
  153. * @param array $params
  154. * @param CacheKey $cache_key
  155. */
  156. abstract protected function fetchAll($data, $params = array(), $cache_key = null);
  157. }