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.

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