Skip to content

Commit

Permalink
#14 when no where filter is used, don't apply
Browse files Browse the repository at this point in the history
- added check for empty or
  • Loading branch information
metaclass-nl committed Sep 20, 2022
1 parent e316cc9 commit aaaa1e3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/Filter/FilterLogic.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $q
// Problem: too hard to add the joins from the extensions and correctly initialize the QueryNameGenerator
// Workaround may fail if extensions did any joins and filters also, or if both use the QueryNameGenerator

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

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

// replace by marker expression
Expand Down Expand Up @@ -347,7 +350,7 @@ protected function replaceInnerJoinsByLeftJoins(QueryBuilder $queryBuilder) {
$joinExp->getConditionType(),
$joinExp->getCondition(),
$joinExp->getIndexBy()
);
);
} else {
$result[$rootAlias][$i] = $joinExp;
}
Expand Down
17 changes: 17 additions & 0 deletions tests/Filter/FilterLogicWithAnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,23 @@ public function testNoLogic()
'DQL');
}

public function testOrNoFilter()
{
$reqData = null;
parse_str('or', $reqData);
// var_dump($reqData);
$context = ['filters' => $reqData];
foreach ($this->filters as $filter) {
$filter->apply($this->testEntityQb, $this->queryNameGen, TestEntity::class, 'get', $context);
}

$this->assertEquals(
str_replace('
', '', "SELECT o FROM Metaclass\FilterBundle\Entity\TestEntity o"),
$this->testEntityQb->getDQL(),
'DQL');
}

public function testDdFilterAnd()
{
$reqData = null;
Expand Down

0 comments on commit aaaa1e3

Please sign in to comment.