@@ -171,6 +171,34 @@ protected function setReturnedFieldNames()
171
171
}
172
172
}
173
173
174
+ /**
175
+ * Resolves the column name
176
+ *
177
+ * @param int|string $column The column index or name, depending on `asObject`
178
+ * @return string|null The resolved column name or NULL if out of bounds
179
+ */
180
+ protected function resolveColumnName ($ column )
181
+ {
182
+ if ( ! $ this ->asObject ) {
183
+ $ fieldIndex = $ this ->sequenceNumber ? $ column - 1 : $ column ;
184
+
185
+ // Skip sequence number and extra column
186
+ if (
187
+ ($ this ->sequenceNumber && $ column == 0 ) OR
188
+ $ fieldIndex > count ($ this ->returnedFieldNames ) - 1
189
+ ) return NULL ;
190
+
191
+ $ column = $ this ->returnedFieldNames [$ fieldIndex ];
192
+ }
193
+
194
+ // Checking if it using a column alias
195
+ $ column = isset ($ this ->columnAliases [$ column ])
196
+ ? $ this ->columnAliases [$ column ]
197
+ : $ column ;
198
+
199
+ return $ column ;
200
+ }
201
+
174
202
/**
175
203
* Add sequence number to the output
176
204
* @param string $key Used when returning object output as the key
@@ -188,84 +216,48 @@ public function addSequenceNumber($key = 'sequenceNumber')
188
216
protected function filter ()
189
217
{
190
218
$ globalSearch = [];
191
- $ columnSearch = [];
192
-
193
- $ fieldNamesLength = count ($ this ->returnedFieldNames );
194
219
195
220
// Global column filtering
196
221
if ($ this ->request ->get ('search ' ) && ($ keyword = $ this ->request ->get ('search ' )['value ' ]) != '' ) {
197
222
foreach ($ this ->request ->get ('columns ' , []) as $ request_column ) {
198
223
if (filter_var ($ request_column ['searchable ' ], FILTER_VALIDATE_BOOLEAN )) {
199
224
$ column = $ request_column ['data ' ];
200
-
201
- if ( ! $ this ->asObject ) {
202
- // Skip sequence number
203
- if ($ this ->sequenceNumber && $ column == 0 ) continue ;
204
-
205
- $ fieldIndex = $ this ->sequenceNumber ? $ column - 1 : $ column ;
206
-
207
- // Skip extra column
208
- if ($ fieldIndex > $ fieldNamesLength - 1 ) break ;
209
-
210
- $ column = $ this ->returnedFieldNames [$ fieldIndex ];
211
- }
212
-
213
- // Checking if it using a column alias
214
- $ column = isset ($ this ->columnAliases [$ column ])
215
- ? $ this ->columnAliases [$ column ]
216
- : $ column ;
225
+ $ column = $ this ->resolveColumnName ($ column );
217
226
218
- $ globalSearch [] = sprintf ("`%s` LIKE '%%%s%%' " , $ column , $ keyword );
227
+ if (empty ($ column )) continue ;
228
+
229
+ $ globalSearch [] = $ column ;
219
230
}
220
231
}
221
232
}
222
233
234
+ // Apply global search criteria
235
+ if (!empty ($ globalSearch )) {
236
+ $ this ->queryBuilder ->{$ this ->config ->get ('groupStart ' )}();
237
+
238
+ foreach ($ globalSearch as $ column ) {
239
+ $ this ->queryBuilder ->{$ this ->config ->get ('orLike ' )}($ column , $ keyword );
240
+ }
241
+
242
+ $ this ->queryBuilder ->{$ this ->config ->get ('groupEnd ' )}();
243
+ }
244
+
223
245
// Individual column filtering
224
246
foreach ($ this ->request ->get ('columns ' , []) as $ request_column ) {
225
247
if (
226
248
filter_var ($ request_column ['searchable ' ], FILTER_VALIDATE_BOOLEAN ) &&
227
249
($ keyword = $ request_column ['search ' ]['value ' ]) != ''
228
250
) {
229
251
$ column = $ request_column ['data ' ];
252
+ $ column = $ this ->resolveColumnName ($ column );
253
+
254
+ if (empty ($ column )) continue ;
230
255
231
- if ( ! $ this ->asObject ) {
232
- // Skip sequence number
233
- if ($ this ->sequenceNumber && $ column == 0 ) continue ;
234
-
235
- $ fieldIndex = $ this ->sequenceNumber ? $ column - 1 : $ column ;
236
-
237
- // Skip extra column
238
- if ($ fieldIndex > $ fieldNamesLength - 1 ) break ;
239
-
240
- $ column = $ this ->returnedFieldNames [$ fieldIndex ];
241
- }
242
-
243
- // Checking if it using a column alias
244
- $ column = isset ($ this ->columnAliases [$ column ])
245
- ? $ this ->columnAliases [$ column ]
246
- : $ column ;
247
-
248
- $ columnSearch [] = sprintf ("`%s` LIKE '%%%s%%' " , $ column , $ keyword );
256
+ // Apply column-specific search criteria
257
+ $ this ->queryBuilder ->{$ this ->config ->get ('like ' )}($ column , $ keyword );
249
258
}
250
259
}
251
260
252
- // Merge global search & column search
253
- $ w_filter = '' ;
254
-
255
- if ( ! empty ($ globalSearch )) {
256
- $ w_filter = '( ' . implode (' OR ' , $ globalSearch ) . ') ' ;
257
- }
258
-
259
- if ( ! empty ($ columnSearch )) {
260
- $ w_filter = $ w_filter === '' ?
261
- implode (' AND ' , $ columnSearch ) :
262
- $ w_filter . ' AND ' . implode (' AND ' , $ columnSearch );
263
- }
264
-
265
- if ($ w_filter !== '' ) {
266
- $ this ->queryBuilder ->{$ this ->config ->get ('where ' )}($ w_filter );
267
- }
268
-
269
261
$ this ->recordsFiltered = $ this ->queryBuilder ->{$ this ->config ->get ('countAllResults ' )}('' , FALSE );
270
262
}
271
263
@@ -275,35 +267,20 @@ protected function filter()
275
267
protected function order ()
276
268
{
277
269
if ($ this ->request ->get ('order ' ) && count ($ this ->request ->get ('order ' ))) {
278
- $ orders = [];
279
- $ fieldNamesLength = count ($ this ->returnedFieldNames );
280
-
281
270
foreach ($ this ->request ->get ('order ' ) as $ order ) {
282
271
$ column_idx = $ order ['column ' ];
283
272
$ request_column = $ this ->request ->get ('columns ' )[$ column_idx ];
284
273
285
274
if (filter_var ($ request_column ['orderable ' ], FILTER_VALIDATE_BOOLEAN )) {
286
275
$ column = $ request_column ['data ' ];
276
+ $ column = $ this ->resolveColumnName ($ column );
287
277
288
- if ( ! $ this ->asObject ) {
289
- // Skip sequence number
290
- if ($ this ->sequenceNumber && $ column == 0 ) continue ;
291
-
292
- $ fieldIndex = $ this ->sequenceNumber ? $ column - 1 : $ column ;
293
-
294
- // Skip extra column
295
- if ($ fieldIndex > $ fieldNamesLength - 1 ) break ;
296
-
297
- $ column = $ this ->returnedFieldNames [$ fieldIndex ];
298
- }
278
+ if (empty ($ column )) continue ;
299
279
300
- $ orders [] = sprintf ('`%s` %s ' , $ column , strtoupper ($ order ['dir ' ]));
280
+ // Apply order
281
+ $ this ->queryBuilder ->{$ this ->config ->get ('orderBy ' )}($ column , $ order ['dir ' ]);
301
282
}
302
283
}
303
-
304
- if (!empty ($ orders )) {
305
- $ this ->queryBuilder ->{$ this ->config ->get ('orderBy ' )}(implode (', ' , $ orders ));
306
- }
307
284
}
308
285
}
309
286
0 commit comments