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.

172 lines
4.4 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 2011-11-11
  10. */
  11. abstract class SqlModel extends Model
  12. {
  13. /**
  14. * @param string $ident
  15. * @return string Quoted identifier.
  16. */
  17. public function identify($ident)
  18. {
  19. return $this->db->quoteIdentifier($ident);
  20. }
  21. /**
  22. * @param mixed $value
  23. * @return string Quoted value.
  24. */
  25. public function quote($value)
  26. {
  27. return $this->db->quote($value);
  28. }
  29. /**
  30. * @param int $id
  31. * @return object
  32. */
  33. public function get($id)
  34. {
  35. $sql = 'SELECT * FROM :table WHERE :pk=?';
  36. return $this->fetch($sql, $id);
  37. }
  38. /**
  39. * @param array $data
  40. * @param mixed $where
  41. * @return int Number of affected rows
  42. */
  43. public function update($data, $where)
  44. {
  45. if (is_int($where) || $where === (string) (int) $where) {
  46. $where = $this->identify($this->key) . '=' . (int) $where;
  47. }
  48. return parent::update($data, $where);
  49. }
  50. /**
  51. * @param int $id Int id
  52. * @return int Number of affected rows
  53. */
  54. public function delete($id)
  55. {
  56. $where = $this->identify($this->key) . '=' . (int) $id;
  57. return $this->db->delete($this->table(), $where);
  58. }
  59. /**
  60. * Creates order sql string
  61. *
  62. * @param array $params
  63. * @param array $sortable
  64. * @return string
  65. */
  66. protected function order($params, $sortable = array('id'))
  67. {
  68. $sql = '';
  69. if (isset($params['sort'])) {
  70. $order = (isset($params['order']) && $params['order'] == 'desc') ? 'DESC' : 'ASC';
  71. if (in_array($params['sort'], $sortable)) {
  72. $sql = ' ORDER BY ' . $this->identify($params['sort']) . ' ' . $order;
  73. }
  74. }
  75. return $sql;
  76. }
  77. /**
  78. * Searches using like
  79. *
  80. * @param array $params
  81. * @param array $searchable
  82. * @param string $table_prefix
  83. * @return string
  84. */
  85. protected function search($params, $searchable = array('id'), $table_prefix = '')
  86. {
  87. $sql = '';
  88. if (isset($params['q']) && isset($params['qt']) && in_array($params['qt'], $searchable)) {
  89. if ($table_prefix) {
  90. $sql = $table_prefix . '.';
  91. }
  92. $sql .= $this->identify($params['qt']) . ' LIKE ' . $this->quote('%' . $params['q'] . '%');
  93. }
  94. return $sql;
  95. }
  96. /**
  97. * This method appends to params table and primary key.
  98. * So they can be accessed thru `:table` and `:pk` placeholders.
  99. *
  100. * @return DbStatement
  101. */
  102. protected function query($sql, $params = array())
  103. {
  104. if (!is_array($params)) {
  105. $params = array($params);
  106. }
  107. $params = array(
  108. 'table' => new DbExpr($this->identify($this->table())),
  109. 'pk' => new DbExpr($this->identify($this->key)),
  110. ) + $params;
  111. return $this->db->query($sql, $params);
  112. }
  113. /**
  114. * @param string $sql
  115. * @param array $params
  116. * @param string $field
  117. * @param CacheKey $cache_key
  118. */
  119. protected function fetchField($data, $params = array(), $field, $cache_key = null)
  120. {
  121. if (!$cache_key || !$result = $cache_key->get()) {
  122. $result = $this->query($data, $params)->fetchField($field);
  123. if ($cache_key) {
  124. $cache_key->set($result);
  125. }
  126. }
  127. return $result;
  128. }
  129. /**
  130. * @param string $sql
  131. * @param array $params
  132. * @param CacheKey $cache_key
  133. */
  134. protected function fetch($data, $params = array(), $cache_key = null)
  135. {
  136. if (!$cache_key || !$result = $cache_key->get()) {
  137. $result = $this->query($data, $params)->fetch();
  138. if ($cache_key) {
  139. $cache_key->set($result);
  140. }
  141. }
  142. return $result;
  143. }
  144. /**
  145. * @param string $sql
  146. * @param array $params
  147. * @param CacheKey $cache_key
  148. */
  149. protected function fetchAll($data, $params = array(), $cache_key = null)
  150. {
  151. if (!$cache_key || !$result = $cache_key->get()) {
  152. $result = $this->query($data, $params)->fetchAll();
  153. if ($cache_key) {
  154. $cache_key->set($result);
  155. }
  156. }
  157. return $result;
  158. }
  159. }