@@ -411,45 +411,77 @@ private function setJoins(QueryBuilder $qb)
411411 */
412412 private function setWhere (QueryBuilder $ qb )
413413 {
414+ // Retrieving parameter counter
415+ $ parameterCounter = self ::INIT_PARAMETER_COUNTER ;
416+
414417 // global filtering
415418 if (isset ($ this ->requestParams ['search ' ]) && '' != $ this ->requestParams ['search ' ]['value ' ]) {
416- $ orExpr = $ qb ->expr ()->orX ();
417419
418420 $ globalSearch = $ this ->requestParams ['search ' ]['value ' ];
419421 $ globalSearchType = $ this ->options ->getGlobalSearchType ();
420422
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 ];
431441 }
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+ }
435469 }
436470 }
437471
438472 // individual filtering
439473 if (true === $ this ->accessor ->getValue ($ this ->options , 'individualFiltering ' )) {
440474 $ andExpr = $ qb ->expr ()->andX ();
441475
442- $ parameterCounter = DatatableQueryBuilder::INIT_PARAMETER_COUNTER ;
443-
444476 foreach ($ this ->columns as $ key => $ column ) {
445477 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 ' ])) {
447479 continue ;
448480 }
449481
450482 $ searchValue = $ this ->requestParams ['columns ' ][$ key ]['search ' ]['value ' ];
451483
452- if ('' != $ searchValue && 'null ' != $ searchValue ) {
484+ if ('' !== $ searchValue && 'null ' != = $ searchValue ) {
453485 /** @var FilterInterface $filter */
454486 $ filter = $ this ->accessor ->getValue ($ column , 'filter ' );
455487 $ searchField = $ this ->searchColumns [$ key ];
0 commit comments