Skip to content

Commit

Permalink
add setTotalRecords method
Browse files Browse the repository at this point in the history
  • Loading branch information
n1crack committed Apr 15, 2024
1 parent bbffe61 commit 2655426
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
36 changes: 35 additions & 1 deletion src/Datatables.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ class Datatables
*/
protected $distinctData = [];

/**
* @var array
*/
private $queries = [];

/**
* @var int
*/
private $recordsTotal;

/**
* Datatables constructor.
*
Expand Down Expand Up @@ -234,28 +244,52 @@ private function getDistinctData(): array
return $output ?? [];
}

public function setTotalRecords(int $total): Datatables
{
$this->recordsTotal = $total;

return $this;
}

/**
*
*/
public function setResponseData(): void
{
$this->queries = [];
$this->response['draw'] = $this->options->draw();
$this->response['recordsTotal'] = $this->db->count($this->builder->query);

if (is_null($this->recordsTotal)) {
$this->response['recordsTotal'] = $this->db->count($this->builder->query);
$this->queries['query'] = $this->builder->query;
} else {
$this->response['recordsTotal'] = $this->recordsTotal;
}

if($this->builder->query->sql === $this->builder->filtered->sql) {
$this->response['recordsFiltered'] = $this->response['recordsTotal'];
} else {
$this->response['recordsFiltered'] = $this->db->count($this->builder->filtered);
$this->queries['recordsFiltered'] = $this->builder->filtered;
}

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

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

/**
* @return array
*/
public function queries(): array
{
return $this->queries;
}

/**
* @return string
*/
Expand Down
19 changes: 18 additions & 1 deletion tests/unit/DatatablesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ public function testReturnsRecordCounts()
$this->assertSame(8, $datatables['recordsFiltered']);
}

public function testReturnsRecordCountsThatSetWithoutRunningAnotherQuery()
{
$this->db->query('select id as fid, name, surname, age from mytable where id > 3');

$datatables = $this->db->generate()->toArray();

$this->assertSame(2, count($this->db->queries()));
$this->assertSame(8, $datatables['recordsTotal']);
$this->assertSame(8, $datatables['recordsFiltered']);

$this->db->setTotalRecords(8);
$datatables = $this->db->generate()->toArray();

$this->assertSame(1, count($this->db->queries()));
$this->assertSame(8, $datatables['recordsTotal']);
$this->assertSame(8, $datatables['recordsFiltered']);
}

public function testReturnsDataFromABasicSql()
{
$this->db->query('select id as fid, name, surname, age from mytable');
Expand Down Expand Up @@ -155,7 +173,6 @@ public function testFiltersDataViaGlobalSearch()

$this->assertSame(11, $datatables['recordsTotal']);
$this->assertSame(2, $datatables['recordsFiltered']);

}

public function testSortsDataViaSorting()
Expand Down

0 comments on commit 2655426

Please sign in to comment.