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.

171 lines
3.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. */
  11. abstract class Model
  12. {
  13. /**
  14. * DbDriver instance
  15. *
  16. * @var DbDriver
  17. */
  18. protected $db;
  19. /**
  20. * Cache instance
  21. *
  22. * @var Cache
  23. */
  24. protected $cache;
  25. /**
  26. * Custom expiration time for keys
  27. *
  28. * @var mixed
  29. */
  30. protected $cache_keys = array();
  31. /**
  32. * Caches to clean.
  33. *
  34. * @var mixed
  35. */
  36. protected $caches_clean = array();
  37. protected $table;
  38. protected $key = 'id';
  39. public function __construct($connection = 'default')
  40. {
  41. $this->db = Db::connect($connection);
  42. }
  43. /**
  44. * @return int
  45. */
  46. public function getInsertId()
  47. {
  48. return $this->db->getInsertId($this->table(), $this->key);
  49. }
  50. /**
  51. * @param array $data
  52. * @return int Id of inserted row
  53. */
  54. public function insert($data)
  55. {
  56. $affected = $this->db->insert($this->table(), $data);
  57. return ($this->getInsertId()) ? $this->getInsertId() : $affected;
  58. }
  59. /**
  60. * @param array $data
  61. * @param mixed $where
  62. * @return int Number of affected rows
  63. */
  64. public function update($data, $where)
  65. {
  66. return $this->db->update($this->table(), $data, $where);
  67. }
  68. /**
  69. * @return string
  70. */
  71. protected function table()
  72. {
  73. if (!$this->table) {
  74. $this->table = substr(strtolower(get_class($this)), 0, -5/*strlen('Model')*/);
  75. }
  76. return $this->table;
  77. }
  78. /* Cache workaround */
  79. /**
  80. * @return Cache
  81. */
  82. public function getCache()
  83. {
  84. if (!$this->cache) {
  85. $this->cache = Cacher::get(Config::get(__CLASS__, 'MemcacheCache'));
  86. }
  87. return $this->cache;
  88. }
  89. /**
  90. * @param string $name
  91. * @param array $params
  92. * @return CacheKey
  93. */
  94. protected function cacheKey($name, $params = array())
  95. {
  96. $expire = (isset($this->cache_keys[$name])) ? ($this->cache_keys[$name] * 60) : 0;
  97. return new CacheKey($this->getCache(), $name, $params, $expire);
  98. }
  99. /**
  100. * @param CacheKey $cache
  101. */
  102. protected function addCleanCache($cache)
  103. {
  104. $this->caches_clean[] = $cache;
  105. }
  106. protected function cleanCaches()
  107. {
  108. // cleaning caches
  109. foreach ($this->caches_clean as $cache) {
  110. $cache->del();
  111. }
  112. $this->caches_clean = array();
  113. }
  114. /**
  115. * Abstract methods
  116. */
  117. /**
  118. * @param int $id
  119. * @return object
  120. */
  121. abstract public function get($id);
  122. /**
  123. * @param int $id Int id
  124. * @return int Number of affected rows
  125. */
  126. abstract public function delete($id);
  127. /**
  128. * @param string $data Request
  129. * @param array $params Request parameters
  130. * @param string $field Requested field name
  131. * @param CacheKey $cache_key Key for caching in
  132. */
  133. abstract protected function fetchField($data, $params = array(), $field, $cache_key = null);
  134. /**
  135. * @param string $data Request
  136. * @param array $params Request parameters
  137. * @param CacheKey $cache_key Key for caching in
  138. */
  139. abstract protected function fetch($data, $params = array(), $cache_key = null);
  140. /**
  141. * @param string $data
  142. * @param array $params
  143. * @param CacheKey $cache_key
  144. */
  145. abstract protected function fetchAll($data, $params = array(), $cache_key = null);
  146. }