1 | <?php |
||
67 | class SqlDataProvider extends BaseDataProvider |
||
68 | { |
||
69 | /** |
||
70 | * @var Connection|array|string the DB connection object or the application component ID of the DB connection. |
||
71 | * Starting from version 2.0.2, this can also be a configuration array for creating the object. |
||
72 | */ |
||
73 | public $db = 'db'; |
||
74 | /** |
||
75 | * @var string the SQL statement to be used for fetching data rows. |
||
76 | */ |
||
77 | public $sql; |
||
78 | /** |
||
79 | * @var array parameters (name=>value) to be bound to the SQL statement. |
||
80 | */ |
||
81 | public $params = []; |
||
82 | /** |
||
83 | * @var string|callable the column that is used as the key of the data models. |
||
84 | * This can be either a column name, or a callable that returns the key value of a given data model. |
||
85 | * |
||
86 | * If this is not set, the keys of the [[models]] array will be used. |
||
87 | */ |
||
88 | public $key; |
||
89 | |||
90 | |||
91 | /** |
||
92 | * Initializes the DB connection component. |
||
93 | * This method will initialize the [[db]] property to make sure it refers to a valid DB connection. |
||
94 | * @throws InvalidConfigException if [[db]] is invalid. |
||
95 | */ |
||
96 | 2 | public function init() |
|
104 | |||
105 | /** |
||
106 | * @inheritdoc |
||
107 | */ |
||
108 | 1 | protected function prepareModels() |
|
109 | { |
||
110 | 1 | $sort = $this->getSort(); |
|
111 | 1 | $pagination = $this->getPagination(); |
|
112 | 1 | if ($pagination === false && $sort === false) { |
|
113 | return $this->db->createCommand($this->sql, $this->params)->queryAll(); |
||
114 | } |
||
115 | |||
116 | 1 | $sql = $this->sql; |
|
117 | 1 | $orders = []; |
|
118 | 1 | $limit = $offset = null; |
|
119 | |||
120 | 1 | if ($sort !== false) { |
|
121 | 1 | $orders = $sort->getOrders(); |
|
122 | 1 | $pattern = '/\s+order\s+by\s+([\w\s,\.]+)$/i'; |
|
123 | 1 | if (preg_match($pattern, $sql, $matches)) { |
|
124 | array_unshift($orders, new Expression($matches[1])); |
||
125 | $sql = preg_replace($pattern, '', $sql); |
||
126 | } |
||
127 | } |
||
128 | |||
129 | 1 | if ($pagination !== false) { |
|
130 | 1 | $pagination->totalCount = $this->getTotalCount(); |
|
131 | 1 | $limit = $pagination->getLimit(); |
|
132 | 1 | $offset = $pagination->getOffset(); |
|
133 | } |
||
134 | |||
135 | 1 | $sql = $this->db->getQueryBuilder()->buildOrderByAndLimit($sql, $orders, $limit, $offset); |
|
136 | |||
137 | return $this->db->createCommand($sql, $this->params)->queryAll(); |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @inheritdoc |
||
142 | */ |
||
143 | protected function prepareKeys($models) |
||
144 | { |
||
145 | $keys = []; |
||
146 | if ($this->key !== null) { |
||
147 | foreach ($models as $model) { |
||
148 | if (is_string($this->key)) { |
||
149 | $keys[] = $model[$this->key]; |
||
150 | } else { |
||
151 | $keys[] = call_user_func($this->key, $model); |
||
152 | } |
||
153 | } |
||
154 | |||
155 | return $keys; |
||
156 | } |
||
157 | |||
158 | return array_keys($models); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * @inheritdoc |
||
163 | */ |
||
164 | 2 | protected function prepareTotalCount() |
|
168 | } |
||
169 |