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.

155 lines
3.3 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. protected $table;
  28. protected $connection = 'default';
  29. protected $key = 'id';
  30. public function __construct()
  31. {
  32. $this->db = Db::connect($this->connection);
  33. }
  34. /**
  35. * @return int
  36. */
  37. public function getInsertId()
  38. {
  39. return $this->db->getInsertId($this->table(false), $this->key);
  40. }
  41. /**
  42. * @param string $ident
  43. * @return string Quoted identifier.
  44. */
  45. public function identify($ident)
  46. {
  47. return $this->db->quoteIdentifier($ident);
  48. }
  49. /**
  50. * @param mixed $value
  51. * @return string Quoted value.
  52. */
  53. public function quote($value)
  54. {
  55. return $this->db->quote($value);
  56. }
  57. /**
  58. * @param int $id
  59. * @return object
  60. */
  61. public function get($id)
  62. {
  63. $sql = 'SELECT * FROM ' . $this->table() . ' WHERE ' . $this->identify($this->key) . '=' . (int) $id;
  64. return $this->db->query($sql)->fetch();
  65. }
  66. /**
  67. * @param array $data
  68. * @return int Affect row or id of inserted field.
  69. */
  70. public function save($data)
  71. {
  72. $key = isset($data[$this->key]) ? $data[$this->key] : false;
  73. $result = false;
  74. if ($key) {
  75. unset($data[$this->key]);
  76. $result = $this->update($data, $this->identify($this->key) . '=' . (int) $id);
  77. } else {
  78. $result = $this->insert($data);
  79. }
  80. return $result;
  81. }
  82. /**
  83. * @param array $data
  84. * @return int Id of inserted row
  85. */
  86. public function insert($data)
  87. {
  88. if (!$this->db->insert($this->table(false), $data)) {
  89. return false;
  90. }
  91. return $this->getInsertId();
  92. }
  93. /**
  94. * @param array $data
  95. * @param mixed $where
  96. * @return int Number of affected rows
  97. */
  98. public function update($data, $where)
  99. {
  100. if (is_int($where)) {
  101. $where = $this->identify($this->key) . '=' . (int) $where;
  102. }
  103. return $this->db->update($this->table(false), $data, $where);
  104. }
  105. /**
  106. * @param mixed $where
  107. * @return int Number of affected rows
  108. */
  109. public function delete($where)
  110. {
  111. if (is_int($where)) {
  112. $where = $this->identify($this->key) . '=' . (int) $where;
  113. }
  114. return $this->db->delete($this->table(false), $where);
  115. }
  116. /**
  117. * @param bool $autoindent
  118. * @return string
  119. */
  120. protected function table($autoindent = true)
  121. {
  122. if (!$this->table) {
  123. $this->table = substr(strtolower(get_class($this)), 0, -5/*strlen('Model')*/);
  124. }
  125. return $autoindent ? $this->identify($this->table) : $this->table;
  126. }
  127. /**
  128. * @return Cache
  129. */
  130. protected function cache()
  131. {
  132. if (!$this->cache) {
  133. $this->cache = Cacher::get(Config::get(__CLASS__, 'MemcacheCache'));
  134. }
  135. return $this->cache;
  136. }
  137. }