Skip to content

Commit

Permalink
Use fresh SearchQuery for every getData call
Browse files Browse the repository at this point in the history
  • Loading branch information
JirsaR committed Jan 14, 2020
1 parent 3e98663 commit 8741f3f
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 19 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "hanaboso/data-grid",
"license": "proprietary",
"version": "2.1.1",
"version": "2.1.2",
"description": "Data grid",
"autoload": {
"psr-4": {
Expand Down
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 16 additions & 12 deletions src/GridFilterAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,10 @@ public function __construct(EntityManager $em)
$this->em = $em;
$this->setEntity();

$this->countQuery = $this->configCustomCountQuery();
$this->filterCols = $this->filterCols();
$this->filterColsCallbacks = $this->configFilterColsCallbacks();
$this->orderCols = $this->orderCols();
$this->searchableCols = $this->searchableCols();
$this->searchQuery = $this->prepareSearchQuery();
$this->filterColsCallbacks = $this->configFilterColsCallbacks();
$this->fetchJoin = $this->useFetchJoin();
}

Expand All @@ -117,7 +115,8 @@ public function __construct(EntityManager $em)
*/
public function getData(GridRequestDtoInterface $gridRequestDto, array $dateFields = []): array
{
$this->prepareSearchQuery();
$this->searchQuery = $this->prepareSearchQuery();
$this->countQuery = $this->configCustomCountQuery();
$this->processSortations($gridRequestDto);
$this->processConditions($gridRequestDto, $this->searchQuery);

Expand Down Expand Up @@ -193,8 +192,8 @@ private function processSortations(GridRequestDtoInterface $dto): void
*/
private function processConditions(GridRequestDtoInterface $dto, QueryBuilder $builder): void
{
$conditions = $dto->getFilter();
$advancedConditionExpression = $builder->expr()->andX();
$conditions = $dto->getFilter();
$conditionExpression = $builder->expr()->andX();

$exp = FALSE;
foreach ($conditions as $andCondition) {
Expand Down Expand Up @@ -248,13 +247,13 @@ private function processConditions(GridRequestDtoInterface $dto, QueryBuilder $b
}

if ($hasExpression) {
$advancedConditionExpression = $advancedConditionExpression->add($expression);
$exp = TRUE;
$conditionExpression = $conditionExpression->add($expression);
$exp = TRUE;
}
}

if ($exp) {
$builder->andWhere($advancedConditionExpression);
$builder->andWhere($conditionExpression);
}

$search = $dto->getSearch();
Expand Down Expand Up @@ -364,13 +363,18 @@ abstract protected function orderCols(): array;
abstract protected function searchableCols(): array;

/**
* @return bool
* -------------------------------------------- HELPERS -----------------------------------------------
*/
abstract protected function useFetchJoin(): bool;

/**
* -------------------------------------------- HELPERS -----------------------------------------------
* Whether the query joins a collection (true by default).
*
* @return bool
*/
protected function useFetchJoin(): bool
{
return TRUE;
}

/**
* In child can configure GridFilterAbstract::filterColsCallbacks
Expand Down
2 changes: 2 additions & 0 deletions tests/Filter/EntityFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ protected function searchableCols(): array
*/
protected function useFetchJoin(): bool
{
parent::useFetchJoin();

return TRUE;
}

Expand Down
43 changes: 43 additions & 0 deletions tests/Integration/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2542,4 +2542,47 @@ public function testGetConditionNotBetween(): void
);
}

/**
* @throws Exception
*/
public function testGetDataMissingCountQuery(): void
{
$f = $this->getMockBuilder(EntityFilter::class)
->setMethods(['configCustomCountQuery'])
->setConstructorArgs([$this->em])
->getMock();
$f->method('configCustomCountQuery')->willReturn(NULL);
$this->setProperty($f, 'em', $this->em);

$result = $f->getData(
new GridRequestDto(
[
self::FILTER => [
[
[
'column' => 'string',
'value' => 'ing 1',
'operator' => 'ENDS',
],
],
],
]
),
['date']
);
self::assertEquals(
[
[
'id' => $result[0]['id'],
'string' => 'String 1',
'int' => 1,
'float' => 1.1,
'bool' => FALSE,
'date' => $this->today->modify('1 day')->format(self::DATETIME),
],
],
$result
);
}

}

0 comments on commit 8741f3f

Please sign in to comment.