1
|
|
|
<?php |
2
|
|
|
/******************************************************************************* |
3
|
|
|
* This file is part of the GraphQL Bundle package. |
4
|
|
|
* |
5
|
|
|
* (c) YnloUltratech <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
******************************************************************************/ |
10
|
|
|
|
11
|
|
|
namespace Ynlo\GraphQLBundle\SearchBy\Common; |
12
|
|
|
|
13
|
|
|
use Doctrine\ORM\Query\Expr\Andx; |
14
|
|
|
use Doctrine\ORM\Query\Expr\Orx; |
15
|
|
|
use Doctrine\ORM\QueryBuilder; |
16
|
|
|
use Ynlo\GraphQLBundle\SearchBy\SearchByContext; |
17
|
|
|
use Ynlo\GraphQLBundle\SearchBy\SearchByInterface; |
18
|
|
|
|
19
|
|
|
class SearchByDoctrineColumn implements SearchByInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @inheritDoc |
23
|
|
|
*/ |
24
|
|
|
public function __invoke(SearchByContext $context, QueryBuilder $qb, Orx $orx, $alias, string $column, string $mode, string $search) |
25
|
|
|
{ |
26
|
|
|
while (strpos($column, '.') !== false) { |
27
|
|
|
[$child, $column] = explode('.', $column, 2); |
28
|
|
|
$parentAlias = $alias; |
29
|
|
|
$alias = 'searchJoin'.ucfirst($child); |
30
|
|
|
if (!\in_array($alias, $qb->getAllAliases(), true)) { |
31
|
|
|
$qb->leftJoin("{$parentAlias}.{$child}", $alias); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if (self::PARTIAL_SEARCH === $mode) { |
36
|
|
|
//search each word separate |
37
|
|
|
$searchArray = explode(' ', $search); |
38
|
|
|
|
39
|
|
|
$partialAnd = new Andx(); |
40
|
|
|
foreach ($searchArray as $index => $q) { |
41
|
|
|
$partialAnd->add("$alias.$column LIKE :query_search_$index"); |
42
|
|
|
$qb->setParameter("query_search_$index", '%'.addcslashes($q, '%_').'%'); |
43
|
|
|
} |
44
|
|
|
$orx->add($partialAnd); |
45
|
|
|
} else { |
46
|
|
|
$orx->add("$alias.$column LIKE :query_search"); |
47
|
|
|
$qb->setParameter('query_search', trim($search)); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
} |