Skip to content

Commit

Permalink
Make IdiormResultSet::as_array behave like ORM::as_array
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Jan 24, 2016
1 parent 1db83c9 commit 2728e89
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
12 changes: 10 additions & 2 deletions idiorm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2346,11 +2346,19 @@ public function get_results() {
}

/**
* Get the current result set as an array
* Return the content of the result set
* as an array of associative arrays. Column
* names may optionally be supplied as arguments,
* if so, only those keys will be returned.
* @return array
*/
public function as_array() {
return $this->get_results();
$rows = array();
$args = func_get_args();
foreach($this->_results as $row) {
$rows[] = call_user_func_array(array($row, 'as_array'), $args);
}
return $rows;
}

/**
Expand Down
18 changes: 14 additions & 4 deletions test/IdiormResultSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,20 @@ public function testSetResultsAndGetResults() {
}

public function testAsArray() {
$result_set = array('item' => new stdClass);
$IdiormResultSet = new IdiormResultSet();
$IdiormResultSet->set_results($result_set);
$this->assertSame($IdiormResultSet->as_array(), $result_set);
$result_set = array(
'item' => ORM::for_table('test')->create(array('foo' => 1, 'bar' => 2)),
'item2' => ORM::for_table('test')->create(array('foo' => 3, 'bar' => 4))
);
$IdiormResultSet = new IdiormResultSet($result_set);

$this->assertEquals($IdiormResultSet->as_array(), array(
array('foo' => 1, 'bar' => 2),
array('foo' => 3, 'bar' => 4))
);
$this->assertEquals($IdiormResultSet->as_array('foo'), array(
array('foo' => 1),
array('foo' => 3))
);
}

public function testCount() {
Expand Down
7 changes: 7 additions & 0 deletions test/ORMTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,11 @@ public function testNullPrimaryKeyPart() {
$this->assertEquals($e->getMessage(), 'Primary key ID contains null value(s)');
}
}

public function testAsArray() {
$model = ORM::for_table('test')->create(array('foo' => 1, 'bar' => 2));
$this->assertEquals($model->as_array(), array('foo' => 1, 'bar' => 2));
$this->assertEquals($model->as_array('foo'), array('foo' => 1));
}

}

0 comments on commit 2728e89

Please sign in to comment.