1 | <?php |
||
9 | class TableSearch |
||
10 | { |
||
11 | /** |
||
12 | * @var Table |
||
13 | */ |
||
14 | protected $table; |
||
15 | |||
16 | /** |
||
17 | * |
||
18 | * @var Select |
||
19 | */ |
||
20 | protected $select; |
||
21 | |||
22 | /** |
||
23 | * @var boolean |
||
24 | */ |
||
25 | protected $has_modified_columns = false; |
||
26 | |||
27 | /** |
||
28 | * |
||
29 | * @var array|string |
||
30 | */ |
||
31 | protected $tableIdentifier; |
||
32 | |||
33 | /** |
||
34 | * |
||
35 | * @param Select $select table name |
||
36 | * @param Table $table |
||
37 | */ |
||
38 | 26 | public function __construct(Select $select, Table $table) |
|
44 | |||
45 | /** |
||
46 | * Limit the number of results |
||
47 | * |
||
48 | * @param int $limit |
||
49 | * @return TableSearch |
||
50 | */ |
||
51 | 14 | public function limit($limit) |
|
56 | |||
57 | /** |
||
58 | * Set offset to use when a limit has been set. |
||
59 | * |
||
60 | * @param int $offset |
||
61 | * @return TableSearch |
||
62 | */ |
||
63 | 1 | public function offset($offset) |
|
68 | |||
69 | |||
70 | /** |
||
71 | * Set the table columns to retrieve |
||
72 | * |
||
73 | * @param array $columns array list of columns, key are used as aliases |
||
74 | * @param boolean $prefixColumnsWithTable |
||
75 | * @return TableSearch |
||
76 | */ |
||
77 | 4 | public function columns(array $columns, $prefixColumnsWithTable = true) |
|
83 | |||
84 | /** |
||
85 | * Add table prefixed columns, will automatically |
||
86 | * quote table parts identifiers found in the column name. |
||
87 | * It provides an alternative for defining columns from multiple/joined |
||
88 | * table in one go. |
||
89 | * |
||
90 | * @param array $columns |
||
91 | */ |
||
92 | 3 | public function prefixedColumns(array $columns) |
|
98 | |||
99 | /** |
||
100 | * Set a group option |
||
101 | * |
||
102 | * @param array|string $group a column or an array of columns to group by |
||
103 | * @return TableSearch |
||
104 | */ |
||
105 | 1 | public function group($group) |
|
110 | |||
111 | /** |
||
112 | * Set a habing option |
||
113 | * |
||
114 | * @param Where|\Closure|string|array $predicate |
||
115 | * @param string $combination One of the OP_* constants from Predicate\PredicateSet |
||
116 | * @return Select |
||
117 | |||
118 | * @return TableSearch |
||
119 | */ |
||
120 | 1 | public function having($predicate, $combination = Predicate\PredicateSet::OP_AND) |
|
125 | |||
126 | |||
127 | /** |
||
128 | * Set an order by clause |
||
129 | * |
||
130 | * @param string|array $order a columns or an array definition of columns |
||
131 | * @return TableSearch |
||
132 | */ |
||
133 | 5 | public function order($order) |
|
138 | |||
139 | /** |
||
140 | * Add a where condition |
||
141 | * |
||
142 | * @param Where|\Closure|string|array|Predicate\PredicateInterface $predicate |
||
143 | * @param string $combination One of the OP_* constants from Zend\Db\Sql\Predicate\PredicateSet |
||
144 | * @throws Zend\Db\Sql\Exception\InvalidArgumentException |
||
145 | * @return TableSearch |
||
146 | */ |
||
147 | 9 | public function where($predicate, $combination = null) |
|
152 | |||
153 | /** |
||
154 | * Add an orWhere condition to the search |
||
155 | * |
||
156 | * @param Where|\Closure|string|array|Predicate\PredicateInterface $predicate |
||
157 | * @throws Zend\Db\Sql\Exception\InvalidArgumentException |
||
158 | * @return TableSearch |
||
159 | */ |
||
160 | 1 | public function orWhere($predicate) |
|
165 | |||
166 | |||
167 | /** |
||
168 | * Add an inner table join to the search |
||
169 | * |
||
170 | * @param string|array $table |
||
171 | * @param string $on |
||
172 | * @param string|array $columns by default won't retrieve any column from the joined table |
||
173 | * @return TableSearch |
||
174 | */ |
||
175 | 1 | public function join($table, $on, $columns = []) |
|
185 | |||
186 | /** |
||
187 | * Add an left outer table join to the search |
||
188 | * |
||
189 | * @param string|array $table |
||
190 | * @param string $on |
||
191 | * @param string|array $columns by default won't retrieve any column from the joined table |
||
192 | * @return TableSearch |
||
193 | */ |
||
194 | 3 | public function joinLeft($table, $on, $columns = []) |
|
200 | |||
201 | |||
202 | |||
203 | /** |
||
204 | * Add an right outer table join to the search |
||
205 | * |
||
206 | * @param string|array $table |
||
207 | * @param string $on |
||
208 | * @param string|array $columns by default won't retrieve any column from the joined table |
||
209 | * @return TableSearch |
||
210 | */ |
||
211 | 1 | public function joinRight($table, $on, $columns = []) |
|
217 | |||
218 | |||
219 | |||
220 | /** |
||
221 | * Return the underlying select |
||
222 | * |
||
223 | * @return Select |
||
224 | */ |
||
225 | 1 | public function getSelect() |
|
229 | |||
230 | /** |
||
231 | * Return SQL string |
||
232 | * |
||
233 | * @return string |
||
234 | */ |
||
235 | 4 | public function getSql() |
|
240 | |||
241 | |||
242 | /** |
||
243 | * Return a json version of the results |
||
244 | * |
||
245 | * @return string Json encoded |
||
246 | */ |
||
247 | 1 | public function toJson() |
|
251 | |||
252 | /** |
||
253 | * Return an array version of the results |
||
254 | * |
||
255 | * @return array |
||
256 | */ |
||
257 | 10 | public function toArray() |
|
261 | |||
262 | |||
263 | |||
264 | /** |
||
265 | * Return record as an array |
||
266 | * |
||
267 | * @return ResultSet |
||
268 | */ |
||
269 | 9 | public function execute() |
|
274 | |||
275 | |||
276 | |||
277 | /** |
||
278 | * Return an array indexed by $indexKey |
||
279 | * useful for comboboxes... |
||
280 | * |
||
281 | * @param string $columnKey |
||
282 | * @param string $indexKey |
||
283 | * @return array |
||
284 | */ |
||
285 | 1 | public function toArrayColumn($columnKey, $indexKey) |
|
291 | |||
292 | /** |
||
293 | * |
||
294 | * @param array|string $tableSpec |
||
295 | |||
296 | |||
297 | protected function getPrefixedColumns($tableSpec=null) |
||
298 | { |
||
299 | |||
300 | if ($tableSpec === null) $tableSpec = $this->tableIdentifier; |
||
301 | |||
302 | if (is_array($tableSpec)) { |
||
303 | $alias = key($this->tableIdentifier); |
||
304 | $table = $this->tableIdentifier[$alias]; |
||
305 | } else { |
||
306 | $alias = $this->tableIdentifier; |
||
307 | $table = $alias; |
||
308 | } |
||
309 | |||
310 | $columns = array(); |
||
311 | $pf = $this->table->getTableManager()->getDbAdapter()->getPlatform(); |
||
312 | $cols = array_keys($this->table->getColumnsInformation()); |
||
313 | foreach($cols as $column) { |
||
314 | //$columns["$alias.$column"] = $column; |
||
315 | //$columns[$column] = new \Zend\Db\Sql\TableIdentifier($pf->quoteIdentifier($alias) . $pf->getIdentifierSeparator() . $pf->quoteIdentifier($column)); |
||
316 | //$columns[$column] = |
||
317 | $columns[$column] = new Predicate\Expression($pf->quoteIdentifier($alias) . $pf->getIdentifierSeparator() . $pf->quoteIdentifier($column)); |
||
318 | } |
||
319 | return $columns; |
||
320 | |||
321 | |||
322 | } |
||
323 | */ |
||
324 | /** |
||
325 | * Prefix table join condition |
||
326 | * |
||
327 | * @param string|array $table |
||
328 | * @return array|string |
||
329 | */ |
||
330 | 5 | protected function prefixTableJoinCondition($table) |
|
343 | } |
||
344 |