@@ -17,120 +17,120 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | abstract class AbstractActiveRecordSearch extends AbstractActiveRecord |
| 19 | 19 | { |
| 20 | - /** |
|
| 21 | - * {@inheritdoc} |
|
| 22 | - */ |
|
| 23 | - public function search($where = [], $orderBy = [], $limit = -1, $offset = 0) |
|
| 24 | - { |
|
| 25 | - try { |
|
| 26 | - $pdoStatement = $this->getPdo()->prepare($this->getSearchQuery($where, $orderBy, $limit, $offset)); |
|
| 27 | - array_walk_recursive($where, function(&$value) use ($pdoStatement) { |
|
| 28 | - static $index = 1; |
|
| 29 | - |
|
| 30 | - $pdoStatement->bindParam($index++, $value); |
|
| 31 | - }); |
|
| 32 | - |
|
| 33 | - $pdoStatement->execute(); |
|
| 34 | - $result = []; |
|
| 35 | - |
|
| 36 | - while ($fetch = $pdoStatement->fetch()) { |
|
| 37 | - $new = new static($this->getPdo()); |
|
| 38 | - |
|
| 39 | - $new->setId(intval($fetch['id'])); |
|
| 40 | - $new->setActiveRecordData($fetch); |
|
| 41 | - |
|
| 42 | - $result[] = $new; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - return $result; |
|
| 46 | - } catch (\PDOException $e) { |
|
| 47 | - throw new ActiveRecordException(sprintf('Can not search the record in the `%s` table.', $this->getActiveRecordName()), 0, $e); |
|
| 48 | - } |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Returns the search query with the given where, order by, limit and offset clauses. |
|
| 53 | - * |
|
| 54 | - * @param array $where = [] |
|
| 55 | - * @param array $orderBy = [] |
|
| 56 | - * @param int $limit = -1 |
|
| 57 | - * @param int $offset = 0 |
|
| 58 | - * @return string the search query with the given where, order by, limit and offset clauses. |
|
| 59 | - */ |
|
| 60 | - private function getSearchQuery($where = [], $orderBy = [], $limit = -1, $offset = 0) |
|
| 61 | - { |
|
| 62 | - return sprintf( |
|
| 63 | - 'SELECT * FROM `%s` %s %s %s', |
|
| 64 | - $this->getActiveRecordName(), |
|
| 65 | - $this->getSearchQueryWhereClause($where), |
|
| 66 | - $this->getSearchQueryOrderByClause($orderBy), |
|
| 67 | - $this->getSearchQueryLimitClause($limit, $offset) |
|
| 68 | - ); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Returns the search query where clause. |
|
| 73 | - * |
|
| 74 | - * @param array $where |
|
| 75 | - * @return string the search query where clause. |
|
| 76 | - */ |
|
| 77 | - private function getSearchQueryWhereClause($where) |
|
| 78 | - { |
|
| 79 | - $columns = array_keys($this->getActiveRecordData()); |
|
| 80 | - $columns[] = 'id'; |
|
| 81 | - $result = []; |
|
| 82 | - |
|
| 83 | - foreach ($where as $key => $value) { |
|
| 84 | - if (!in_array($key, $columns)) { |
|
| 85 | - throw new ActiveRecordException(sprintf('Search option key `%s` does not exists.', $key)); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - if (is_numeric($value)) { |
|
| 89 | - $result[] = sprintf('`%s` = ?', $key); |
|
| 90 | - } elseif (is_string($value)) { |
|
| 91 | - $result[] = sprintf('`%s` LIKE ?', $key); |
|
| 92 | - } elseif (is_null($value)) { |
|
| 93 | - $result[] = sprintf('`%s` IS ?', $key); |
|
| 94 | - } elseif (is_array($value) && !empty($value)) { |
|
| 95 | - $result[] = sprintf('`%s` IN (%s)', $key, implode(',', array_fill(0, count($value), '?'))); |
|
| 96 | - } else { |
|
| 97 | - throw new ActiveRecordException(sprintf('Search option value of key `%s` is not supported.', $key)); |
|
| 98 | - } |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - return empty($result) ? '' : 'WHERE ' . implode(' AND ', $result); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * Returns the search query order by clause. |
|
| 106 | - * |
|
| 107 | - * @param array $orderBy |
|
| 108 | - * @return string the search query order by clause. |
|
| 109 | - */ |
|
| 110 | - private function getSearchQueryOrderByClause($orderBy) |
|
| 111 | - { |
|
| 112 | - $result = []; |
|
| 113 | - |
|
| 114 | - foreach ($orderBy as $key => $value) { |
|
| 115 | - $result[] = sprintf('`%s` %s', $key, $value == 'DESC' ? 'DESC' : 'ASC'); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - return empty($result) ? '' : 'ORDER BY ' . implode(', ', $result); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Returns the search query limit and clause. |
|
| 123 | - * |
|
| 124 | - * @param int $limit = -1 |
|
| 125 | - * @param int $offset = 0 |
|
| 126 | - * @return string the search query limit and clause. |
|
| 127 | - */ |
|
| 128 | - private function getSearchQueryLimitClause($limit, $offset) |
|
| 129 | - { |
|
| 130 | - if ($limit == -1) { |
|
| 131 | - return ''; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - return sprintf('LIMIT %d OFFSET %d', $limit, $offset); |
|
| 135 | - } |
|
| 20 | + /** |
|
| 21 | + * {@inheritdoc} |
|
| 22 | + */ |
|
| 23 | + public function search($where = [], $orderBy = [], $limit = -1, $offset = 0) |
|
| 24 | + { |
|
| 25 | + try { |
|
| 26 | + $pdoStatement = $this->getPdo()->prepare($this->getSearchQuery($where, $orderBy, $limit, $offset)); |
|
| 27 | + array_walk_recursive($where, function(&$value) use ($pdoStatement) { |
|
| 28 | + static $index = 1; |
|
| 29 | + |
|
| 30 | + $pdoStatement->bindParam($index++, $value); |
|
| 31 | + }); |
|
| 32 | + |
|
| 33 | + $pdoStatement->execute(); |
|
| 34 | + $result = []; |
|
| 35 | + |
|
| 36 | + while ($fetch = $pdoStatement->fetch()) { |
|
| 37 | + $new = new static($this->getPdo()); |
|
| 38 | + |
|
| 39 | + $new->setId(intval($fetch['id'])); |
|
| 40 | + $new->setActiveRecordData($fetch); |
|
| 41 | + |
|
| 42 | + $result[] = $new; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + return $result; |
|
| 46 | + } catch (\PDOException $e) { |
|
| 47 | + throw new ActiveRecordException(sprintf('Can not search the record in the `%s` table.', $this->getActiveRecordName()), 0, $e); |
|
| 48 | + } |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Returns the search query with the given where, order by, limit and offset clauses. |
|
| 53 | + * |
|
| 54 | + * @param array $where = [] |
|
| 55 | + * @param array $orderBy = [] |
|
| 56 | + * @param int $limit = -1 |
|
| 57 | + * @param int $offset = 0 |
|
| 58 | + * @return string the search query with the given where, order by, limit and offset clauses. |
|
| 59 | + */ |
|
| 60 | + private function getSearchQuery($where = [], $orderBy = [], $limit = -1, $offset = 0) |
|
| 61 | + { |
|
| 62 | + return sprintf( |
|
| 63 | + 'SELECT * FROM `%s` %s %s %s', |
|
| 64 | + $this->getActiveRecordName(), |
|
| 65 | + $this->getSearchQueryWhereClause($where), |
|
| 66 | + $this->getSearchQueryOrderByClause($orderBy), |
|
| 67 | + $this->getSearchQueryLimitClause($limit, $offset) |
|
| 68 | + ); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Returns the search query where clause. |
|
| 73 | + * |
|
| 74 | + * @param array $where |
|
| 75 | + * @return string the search query where clause. |
|
| 76 | + */ |
|
| 77 | + private function getSearchQueryWhereClause($where) |
|
| 78 | + { |
|
| 79 | + $columns = array_keys($this->getActiveRecordData()); |
|
| 80 | + $columns[] = 'id'; |
|
| 81 | + $result = []; |
|
| 82 | + |
|
| 83 | + foreach ($where as $key => $value) { |
|
| 84 | + if (!in_array($key, $columns)) { |
|
| 85 | + throw new ActiveRecordException(sprintf('Search option key `%s` does not exists.', $key)); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + if (is_numeric($value)) { |
|
| 89 | + $result[] = sprintf('`%s` = ?', $key); |
|
| 90 | + } elseif (is_string($value)) { |
|
| 91 | + $result[] = sprintf('`%s` LIKE ?', $key); |
|
| 92 | + } elseif (is_null($value)) { |
|
| 93 | + $result[] = sprintf('`%s` IS ?', $key); |
|
| 94 | + } elseif (is_array($value) && !empty($value)) { |
|
| 95 | + $result[] = sprintf('`%s` IN (%s)', $key, implode(',', array_fill(0, count($value), '?'))); |
|
| 96 | + } else { |
|
| 97 | + throw new ActiveRecordException(sprintf('Search option value of key `%s` is not supported.', $key)); |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + return empty($result) ? '' : 'WHERE ' . implode(' AND ', $result); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * Returns the search query order by clause. |
|
| 106 | + * |
|
| 107 | + * @param array $orderBy |
|
| 108 | + * @return string the search query order by clause. |
|
| 109 | + */ |
|
| 110 | + private function getSearchQueryOrderByClause($orderBy) |
|
| 111 | + { |
|
| 112 | + $result = []; |
|
| 113 | + |
|
| 114 | + foreach ($orderBy as $key => $value) { |
|
| 115 | + $result[] = sprintf('`%s` %s', $key, $value == 'DESC' ? 'DESC' : 'ASC'); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + return empty($result) ? '' : 'ORDER BY ' . implode(', ', $result); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Returns the search query limit and clause. |
|
| 123 | + * |
|
| 124 | + * @param int $limit = -1 |
|
| 125 | + * @param int $offset = 0 |
|
| 126 | + * @return string the search query limit and clause. |
|
| 127 | + */ |
|
| 128 | + private function getSearchQueryLimitClause($limit, $offset) |
|
| 129 | + { |
|
| 130 | + if ($limit == -1) { |
|
| 131 | + return ''; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + return sprintf('LIMIT %d OFFSET %d', $limit, $offset); |
|
| 135 | + } |
|
| 136 | 136 | } |