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