Skip to content

Commit 2655426

Browse files
committed
add setTotalRecords method
1 parent bbffe61 commit 2655426

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

src/Datatables.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ class Datatables
5555
*/
5656
protected $distinctData = [];
5757

58+
/**
59+
* @var array
60+
*/
61+
private $queries = [];
62+
63+
/**
64+
* @var int
65+
*/
66+
private $recordsTotal;
67+
5868
/**
5969
* Datatables constructor.
6070
*
@@ -234,28 +244,52 @@ private function getDistinctData(): array
234244
return $output ?? [];
235245
}
236246

247+
public function setTotalRecords(int $total): Datatables
248+
{
249+
$this->recordsTotal = $total;
250+
251+
return $this;
252+
}
253+
237254
/**
238255
*
239256
*/
240257
public function setResponseData(): void
241258
{
259+
$this->queries = [];
242260
$this->response['draw'] = $this->options->draw();
243-
$this->response['recordsTotal'] = $this->db->count($this->builder->query);
261+
262+
if (is_null($this->recordsTotal)) {
263+
$this->response['recordsTotal'] = $this->db->count($this->builder->query);
264+
$this->queries['query'] = $this->builder->query;
265+
} else {
266+
$this->response['recordsTotal'] = $this->recordsTotal;
267+
}
244268

245269
if($this->builder->query->sql === $this->builder->filtered->sql) {
246270
$this->response['recordsFiltered'] = $this->response['recordsTotal'];
247271
} else {
248272
$this->response['recordsFiltered'] = $this->db->count($this->builder->filtered);
273+
$this->queries['recordsFiltered'] = $this->builder->filtered;
249274
}
250275

251276
$this->response['data'] = $this->getData();
277+
$this->queries['full'] = $this->builder->full;
252278

253279
if (\count($this->distinctColumn) > 0 || \count($this->distinctData) > 0) {
254280
$this->response['distinctData'] = array_merge($this->response['distinctData'] ?? [],
255281
$this->getDistinctData(), $this->distinctData);
256282
}
257283
}
258284

285+
/**
286+
* @return array
287+
*/
288+
public function queries(): array
289+
{
290+
return $this->queries;
291+
}
292+
259293
/**
260294
* @return string
261295
*/

tests/unit/DatatablesTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,24 @@ public function testReturnsRecordCounts()
4545
$this->assertSame(8, $datatables['recordsFiltered']);
4646
}
4747

48+
public function testReturnsRecordCountsThatSetWithoutRunningAnotherQuery()
49+
{
50+
$this->db->query('select id as fid, name, surname, age from mytable where id > 3');
51+
52+
$datatables = $this->db->generate()->toArray();
53+
54+
$this->assertSame(2, count($this->db->queries()));
55+
$this->assertSame(8, $datatables['recordsTotal']);
56+
$this->assertSame(8, $datatables['recordsFiltered']);
57+
58+
$this->db->setTotalRecords(8);
59+
$datatables = $this->db->generate()->toArray();
60+
61+
$this->assertSame(1, count($this->db->queries()));
62+
$this->assertSame(8, $datatables['recordsTotal']);
63+
$this->assertSame(8, $datatables['recordsFiltered']);
64+
}
65+
4866
public function testReturnsDataFromABasicSql()
4967
{
5068
$this->db->query('select id as fid, name, surname, age from mytable');
@@ -155,7 +173,6 @@ public function testFiltersDataViaGlobalSearch()
155173

156174
$this->assertSame(11, $datatables['recordsTotal']);
157175
$this->assertSame(2, $datatables['recordsFiltered']);
158-
159176
}
160177

161178
public function testSortsDataViaSorting()

0 commit comments

Comments
 (0)