Skip to content

Commit 9ac0ae7

Browse files
Surttreffynnon
authored andcommitted
find_many returns an associative array
1 parent 5661a8f commit 9ac0ae7

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

paris.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,19 @@ public function find_one($id=null) {
119119
}
120120

121121
/**
122-
* Wrap Idiorm's find_many method to return
123-
* an array of instances of the class associated
124-
* with this wrapper instead of the raw ORM class.
125-
*/
126-
public function find_many() {
127-
$results = parent::find_many();
128-
foreach($results as $key => $result) {
129-
$results[$key] = $this->_create_model_instance($result);
122+
* Create instances of each row in the result and map
123+
* them to an associative array with the primary IDs as
124+
* the array keys.
125+
* @param array $rows
126+
* @return array
127+
*/
128+
protected function _instances_with_id_as_key($rows) {
129+
$instances = array();
130+
foreach($rows as $row) {
131+
$row = $this->_create_model_instance($this->_create_instance_from_row($row));
132+
$instances[$row->id()] = $row;
130133
}
131-
return $results;
134+
return $instances;
132135
}
133136

134137
/**

test/ParisTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,10 @@ public function testHasManyThroughRelationWithCustomIntermediateModelAndKeyNames
192192
$this->assertEquals($expected, ORM::get_last_query());
193193
}
194194

195+
public function testFindResultSet() {
196+
$result_set = Model::factory('BookFive')->find_result_set();
197+
$this->assertInstanceOf('IdiormResultSet', $result_set);
198+
$this->assertSame(count($result_set), 5);
199+
}
200+
195201
}

test/idiorm.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,23 @@ public function find_many() {
611611
*/
612612
protected function _find_many() {
613613
$rows = $this->_run();
614-
return array_map(array($this, '_create_instance_from_row'), $rows);
614+
return $this->_instances_with_id_as_key($rows);
615+
}
616+
617+
/**
618+
* Create instances of each row in the result and map
619+
* them to an associative array with the primary IDs as
620+
* the array keys.
621+
* @param array $rows
622+
* @return array
623+
*/
624+
protected function _instances_with_id_as_key($rows) {
625+
$instances = array();
626+
foreach($rows as $row) {
627+
$row = $this->_create_instance_from_row($row);
628+
$instances[$row->id()] = $row;
629+
}
630+
return $instances;
615631
}
616632

617633
/**
@@ -1657,7 +1673,7 @@ public function id() {
16571673
* database when save() is called.
16581674
*/
16591675
public function set($key, $value = null) {
1660-
$this->_set_orm_property($key, $value);
1676+
return $this->_set_orm_property($key, $value);
16611677
}
16621678

16631679
/**
@@ -1670,7 +1686,7 @@ public function set($key, $value = null) {
16701686
* @param string|null $value
16711687
*/
16721688
public function set_expr($key, $value = null) {
1673-
$this->_set_orm_property($key, $value, true);
1689+
return $this->_set_orm_property($key, $value, true);
16741690
}
16751691

16761692
/**
@@ -1692,6 +1708,7 @@ protected function _set_orm_property($key, $value = null, $expr = false) {
16921708
$this->_expr_fields[$field] = true;
16931709
}
16941710
}
1711+
return $this;
16951712
}
16961713

16971714
/**

0 commit comments

Comments
 (0)