Skip to content

Commit

Permalink
find_many returns an associative array
Browse files Browse the repository at this point in the history
  • Loading branch information
Surt authored and treffynnon committed Aug 30, 2013
1 parent 5661a8f commit 9ac0ae7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
21 changes: 12 additions & 9 deletions paris.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,19 @@ public function find_one($id=null) {
}

/**
* Wrap Idiorm's find_many method to return
* an array of instances of the class associated
* with this wrapper instead of the raw ORM class.
*/
public function find_many() {
$results = parent::find_many();
foreach($results as $key => $result) {
$results[$key] = $this->_create_model_instance($result);
* Create instances of each row in the result and map
* them to an associative array with the primary IDs as
* the array keys.
* @param array $rows
* @return array
*/
protected function _instances_with_id_as_key($rows) {
$instances = array();
foreach($rows as $row) {
$row = $this->_create_model_instance($this->_create_instance_from_row($row));
$instances[$row->id()] = $row;
}
return $results;
return $instances;
}

/**
Expand Down
6 changes: 6 additions & 0 deletions test/ParisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,10 @@ public function testHasManyThroughRelationWithCustomIntermediateModelAndKeyNames
$this->assertEquals($expected, ORM::get_last_query());
}

public function testFindResultSet() {
$result_set = Model::factory('BookFive')->find_result_set();
$this->assertInstanceOf('IdiormResultSet', $result_set);
$this->assertSame(count($result_set), 5);
}

}
23 changes: 20 additions & 3 deletions test/idiorm.php
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,23 @@ public function find_many() {
*/
protected function _find_many() {
$rows = $this->_run();
return array_map(array($this, '_create_instance_from_row'), $rows);
return $this->_instances_with_id_as_key($rows);
}

/**
* Create instances of each row in the result and map
* them to an associative array with the primary IDs as
* the array keys.
* @param array $rows
* @return array
*/
protected function _instances_with_id_as_key($rows) {
$instances = array();
foreach($rows as $row) {
$row = $this->_create_instance_from_row($row);
$instances[$row->id()] = $row;
}
return $instances;
}

/**
Expand Down Expand Up @@ -1657,7 +1673,7 @@ public function id() {
* database when save() is called.
*/
public function set($key, $value = null) {
$this->_set_orm_property($key, $value);
return $this->_set_orm_property($key, $value);
}

/**
Expand All @@ -1670,7 +1686,7 @@ public function set($key, $value = null) {
* @param string|null $value
*/
public function set_expr($key, $value = null) {
$this->_set_orm_property($key, $value, true);
return $this->_set_orm_property($key, $value, true);
}

/**
Expand All @@ -1692,6 +1708,7 @@ protected function _set_orm_property($key, $value = null, $expr = false) {
$this->_expr_fields[$field] = true;
}
}
return $this;
}

/**
Expand Down

0 comments on commit 9ac0ae7

Please sign in to comment.