Skip to content

Commit

Permalink
Fixed issue #8 (Empty resultset/array/builder)
Browse files Browse the repository at this point in the history
  • Loading branch information
m1ome committed Jun 26, 2015
1 parent 0e71cd2 commit 7052efb
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
56 changes: 56 additions & 0 deletions spec/suite/DataTableSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,24 @@

});

it("should work with empty ResultSet", function() {

$resultset = $this->di->get('modelsManager')
->createQuery("SELECT * FROM \Spec\Models\User WHERE balance < 0")
->execute();

$dataTables = new DataTable();
$response = $dataTables->fromResultSet($resultset)->getResponse();

expect($response)->toBe([
'draw' => null,
'recordsTotal' => 0,
'recordsFiltered' => 0,
'data' => []
]);

});

it("should create a ResultSet", function() {

$resultset = $this->di->get('modelsManager')
Expand All @@ -38,6 +56,24 @@

});

it("should create an empty ArrayAdapter", function() {

$array = $this->di->get('modelsManager')
->createQuery("SELECT * FROM \Spec\Models\User WHERE balance < 0")
->execute()->toArray();

$dataTables = new DataTable();
$response = $dataTables->fromArray($array)->getResponse();

expect($response)->toBe([
'draw' => null,
'recordsTotal' => 0,
'recordsFiltered' => 0,
'data' => []
]);

});

it("should create a ArrayAdapter", function() {

$array = $this->di->get('modelsManager')
Expand All @@ -64,6 +100,26 @@

});

it("should create a from a empty QueryBuilder", function() {

$builder = $this->di->get('modelsManager')
->createBuilder()
->columns('id, name, email, balance')
->from('Spec\Models\User')
->where('balance < 0');

$dataTables = new DataTable();
$response = $dataTables->fromBuilder($builder)->getResponse();

expect($response)->toBe([
'draw' => null,
'recordsTotal' => 0,
'recordsFiltered' => 0,
'data' => []
]);

});

it("should create a QueryBuilder", function() {

$builder = $this->di->get('modelsManager')
Expand Down
4 changes: 2 additions & 2 deletions src/DataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function fromBuilder($builder, $columns = []) {
}

public function fromResultSet($resultSet, $columns = []) {
if(empty($columns)) {
if(empty($columns) && $resultSet->count() > 0) {
$columns = array_keys($resultSet->getFirst()->toArray());
$resultSet->rewind();
}
Expand All @@ -73,7 +73,7 @@ public function fromResultSet($resultSet, $columns = []) {
}

public function fromArray($array, $columns = []) {
if(empty($columns)) {
if(empty($columns) && count($array) > 0) {
$columns = array_keys(current($array));
}

Expand Down

0 comments on commit 7052efb

Please sign in to comment.