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.

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