From 9f7169a1cea51ff22cf4154c5c0e6da35d19c558 Mon Sep 17 00:00:00 2001 From: Jurian Arie <28654085+JurianArie@users.noreply.github.com> Date: Tue, 9 Jul 2024 09:37:13 +0200 Subject: [PATCH 1/3] Make query for filteredRecords when totalRecords was manually set --- src/QueryDataTable.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/QueryDataTable.php b/src/QueryDataTable.php index b6bc0813..aea42844 100644 --- a/src/QueryDataTable.php +++ b/src/QueryDataTable.php @@ -25,6 +25,11 @@ class QueryDataTable extends DataTableAbstract */ protected bool $prepared = false; + /** + * Flag to check if the total records count query has been performed. + */ + protected bool $performedTotalRecordsCount = false; + /** * Query callback for custom pagination using limit without offset. * @@ -157,6 +162,20 @@ public function prepareQuery(): static return $this; } + /** + * Count total items. + */ + public function totalCount(): int + { + if ($this->totalRecords !== null) { + return $this->totalRecords; + } + + $this->performedTotalRecordsCount = true; + + return $this->totalRecords = $this->count(); + } + /** * Counts current query. */ @@ -253,7 +272,7 @@ protected function filterRecords(): void // If no modification between the original query and the filtered one has been made // the filteredRecords equals the totalRecords - if ($this->query == $initialQuery) { + if ($this->query == $initialQuery && $this->performedTotalRecordsCount) { $this->filteredRecords ??= $this->totalRecords; } else { $this->filteredCount(); From 1298c5235475c54f63b6b81d21b33a769e6da069 Mon Sep 17 00:00:00 2001 From: Jurian Arie <28654085+JurianArie@users.noreply.github.com> Date: Tue, 9 Jul 2024 09:48:44 +0200 Subject: [PATCH 2/3] Update QueryDataTableTest.php --- tests/Integration/QueryDataTableTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Integration/QueryDataTableTest.php b/tests/Integration/QueryDataTableTest.php index 5c8d9399..8367a037 100644 --- a/tests/Integration/QueryDataTableTest.php +++ b/tests/Integration/QueryDataTableTest.php @@ -26,7 +26,7 @@ public function it_can_set_total_records() $crawler->assertJson([ 'draw' => 0, 'recordsTotal' => 10, - 'recordsFiltered' => 10, + 'recordsFiltered' => 20, ]); } @@ -37,7 +37,7 @@ public function it_can_set_zero_total_records() $crawler->assertJson([ 'draw' => 0, 'recordsTotal' => 0, - 'recordsFiltered' => 0, + 'recordsFiltered' => 20, ]); } From 4d69cdd289bccd904ffab18831747917cb3b5a70 Mon Sep 17 00:00:00 2001 From: JurianArie <28654085+JurianArie@users.noreply.github.com> Date: Thu, 11 Jul 2024 13:09:08 +0200 Subject: [PATCH 3/3] Add test to show the optimization still works --- tests/Integration/QueryDataTableTest.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/Integration/QueryDataTableTest.php b/tests/Integration/QueryDataTableTest.php index 8367a037..d2b4a028 100644 --- a/tests/Integration/QueryDataTableTest.php +++ b/tests/Integration/QueryDataTableTest.php @@ -55,12 +55,19 @@ public function it_can_set_total_filtered_records() #[Test] public function it_returns_all_records_when_no_parameters_is_passed() { + DB::enableQueryLog(); + $crawler = $this->call('GET', '/query/users'); $crawler->assertJson([ 'draw' => 0, 'recordsTotal' => 20, 'recordsFiltered' => 20, ]); + + DB::disableQueryLog(); + $queryLog = DB::getQueryLog(); + + $this->assertCount(2, $queryLog); } #[Test]