@@ -411,45 +411,77 @@ private function setJoins(QueryBuilder $qb)
411
411
*/
412
412
private function setWhere (QueryBuilder $ qb )
413
413
{
414
+ // Retrieving parameter counter
415
+ $ parameterCounter = self ::INIT_PARAMETER_COUNTER ;
416
+
414
417
// global filtering
415
418
if (isset ($ this ->requestParams ['search ' ]) && '' != $ this ->requestParams ['search ' ]['value ' ]) {
416
- $ orExpr = $ qb ->expr ()->orX ();
417
419
418
420
$ globalSearch = $ this ->requestParams ['search ' ]['value ' ];
419
421
$ globalSearchType = $ this ->options ->getGlobalSearchType ();
420
422
421
- foreach ($ this ->columns as $ key => $ column ) {
422
- if (true === $ this ->isSearchableColumn ($ column )) {
423
- /** @var AbstractFilter $filter */
424
- $ filter = $ this ->accessor ->getValue ($ column , 'filter ' );
425
- $ searchType = $ globalSearchType ;
426
- $ searchField = $ this ->searchColumns [$ key ];
427
- $ searchValue = $ globalSearch ;
428
- $ searchTypeOfField = $ column ->getTypeOfField ();
429
- $ orExpr = $ filter ->addOrExpression ($ orExpr , $ qb , $ searchType , $ searchField , $ searchValue , $ searchTypeOfField , $ key );
430
- }
423
+ // If global search type is a list of key words
424
+ if ($ globalSearchType == 'keywords_string ' ) {
425
+
426
+ // Removing unnecessary spaces at the beginning and at the end
427
+ $ globalSearch = trim ($ globalSearch );
428
+
429
+ // Removing multiples spaces
430
+ $ globalSearch = preg_replace ('!\s+! ' , ' ' , $ globalSearch );
431
+
432
+ // Parsing search value to array of keywords
433
+ $ keywords = explode (' ' , $ globalSearch );
434
+
435
+ // Setting type of search type to 'like'
436
+ $ globalSearchType = 'like ' ;
437
+
438
+ // Else considering only one searchstring
439
+ } else {
440
+ $ keywords = [$ globalSearch ];
431
441
}
432
-
433
- if ($ orExpr ->count () > 0 ) {
434
- $ qb ->andWhere ($ orExpr );
442
+
443
+ // Iterating through each keyword
444
+ foreach ($ keywords as $ k => $ word ) {
445
+
446
+ // Initialising 'OR' expression
447
+ $ orExpr = $ qb ->expr ()->orX ();
448
+
449
+ // Iterating through columns
450
+ foreach ($ this ->columns as $ key => $ column ) {
451
+
452
+ if (true === $ this ->isSearchableColumn ($ column )) {
453
+ /** @var AbstractFilter $filter */
454
+ $ filter = $ this ->accessor ->getValue ($ column , 'filter ' );
455
+ $ searchType = $ globalSearchType ;
456
+ $ searchFields = (array ) $ this ->searchColumns [$ key ];
457
+ $ searchValue = $ keywords [$ k ];
458
+ $ searchTypeOfField = $ column ->getTypeOfField ();
459
+ foreach ($ searchFields as $ searchField ) {
460
+ $ orExpr = $ filter ->addOrExpression ($ orExpr , $ qb , $ searchType , $ searchField , $ searchValue , $ searchTypeOfField , $ parameterCounter );
461
+ }
462
+ }
463
+ }
464
+
465
+ // Adding 'OR' expression to global 'WHERE'
466
+ if ($ orExpr ->count () > 0 ) {
467
+ $ qb ->andWhere ($ orExpr );
468
+ }
435
469
}
436
470
}
437
471
438
472
// individual filtering
439
473
if (true === $ this ->accessor ->getValue ($ this ->options , 'individualFiltering ' )) {
440
474
$ andExpr = $ qb ->expr ()->andX ();
441
475
442
- $ parameterCounter = DatatableQueryBuilder::INIT_PARAMETER_COUNTER ;
443
-
444
476
foreach ($ this ->columns as $ key => $ column ) {
445
477
if (true === $ this ->isSearchableColumn ($ column )) {
446
- if (false === array_key_exists ($ key , $ this ->requestParams ['columns ' ])) {
478
+ if (false === \ array_key_exists ($ key , $ this ->requestParams ['columns ' ])) {
447
479
continue ;
448
480
}
449
481
450
482
$ searchValue = $ this ->requestParams ['columns ' ][$ key ]['search ' ]['value ' ];
451
483
452
- if ('' != $ searchValue && 'null ' != $ searchValue ) {
484
+ if ('' !== $ searchValue && 'null ' != = $ searchValue ) {
453
485
/** @var FilterInterface $filter */
454
486
$ filter = $ this ->accessor ->getValue ($ column , 'filter ' );
455
487
$ searchField = $ this ->searchColumns [$ key ];
0 commit comments