Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
* dev:
  Fixed issue #8 (Empty resultset/array/builder)
  Sample code: should be json array not object.
  • Loading branch information
m1ome committed Jun 26, 2015
2 parents a2ea81d + 7052efb commit 9578825
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ class User extends \Phalcon\Mvc\Model {
url: '/test/index',
method: 'POST'
},
columns: {
columns: [
{data: "id", searchable: false},
{data: "name"},
{data: "email"},
{data: "balance", searchable: false}
}
]
});
});
</script>
Expand Down
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 9578825

Please sign in to comment.