Skip to content

Commit aaaa1e3

Browse files
committed
#14 when no where filter is used, don't apply
- added check for empty or
1 parent e316cc9 commit aaaa1e3

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/Filter/FilterLogic.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $q
8989
// Problem: too hard to add the joins from the extensions and correctly initialize the QueryNameGenerator
9090
// Workaround may fail if extensions did any joins and filters also, or if both use the QueryNameGenerator
9191

92-
$filters = $this->getFilters($resourceClass, $operationName, true);
92+
$filters = $this->getFilters($resourceClass, $operationName, true);
9393
foreach ($filters as $filter) {
9494
$filter->apply($newQb, $newQng, $resourceClass, $operationName, $context);
9595
}
@@ -154,7 +154,7 @@ public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $q
154154
throw new \RuntimeException("Could not replace criteria from filters");
155155
}
156156

157-
/**
157+
/**
158158
* @return array of Doctrine\ORM\Query\Expr\* and/or string (DQL),
159159
* each of which must be self-contained in the sense that the intended
160160
* logic is not compromised if it is combined with the others and other
@@ -183,6 +183,9 @@ public function generateExpressions(QueryBuilder $queryBuilder, QueryNameGenerat
183183
*/
184184
protected function doGenerate($queryBuilder, $queryNameGenerator, $resourceClass, $operationName, $context)
185185
{
186+
if (empty($context['filters'])) {
187+
return [];
188+
}
186189
$oldWhere = $queryBuilder->getDQLPart('where');
187190

188191
// replace by marker expression
@@ -347,7 +350,7 @@ protected function replaceInnerJoinsByLeftJoins(QueryBuilder $queryBuilder) {
347350
$joinExp->getConditionType(),
348351
$joinExp->getCondition(),
349352
$joinExp->getIndexBy()
350-
);
353+
);
351354
} else {
352355
$result[$rootAlias][$i] = $joinExp;
353356
}

tests/Filter/FilterLogicWithAnnotationTest.php

+17
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,23 @@ public function testNoLogic()
7373
'DQL');
7474
}
7575

76+
public function testOrNoFilter()
77+
{
78+
$reqData = null;
79+
parse_str('or', $reqData);
80+
// var_dump($reqData);
81+
$context = ['filters' => $reqData];
82+
foreach ($this->filters as $filter) {
83+
$filter->apply($this->testEntityQb, $this->queryNameGen, TestEntity::class, 'get', $context);
84+
}
85+
86+
$this->assertEquals(
87+
str_replace('
88+
', '', "SELECT o FROM Metaclass\FilterBundle\Entity\TestEntity o"),
89+
$this->testEntityQb->getDQL(),
90+
'DQL');
91+
}
92+
7693
public function testDdFilterAnd()
7794
{
7895
$reqData = null;

0 commit comments

Comments
 (0)