Skip to content

Commit 7b47430

Browse files
committed
sharing some work on model instances
1 parent 87a5cf3 commit 7b47430

File tree

1 file changed

+74
-11
lines changed

1 file changed

+74
-11
lines changed

system/database/DB_result.php

Lines changed: 74 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@
2828
*/
2929
class CI_DB_result {
3030

31-
var $conn_id = NULL;
32-
var $result_id = NULL;
33-
var $result_array = array();
34-
var $result_object = array();
35-
var $current_row = 0;
36-
var $num_rows = 0;
37-
var $row_data = NULL;
31+
var $conn_id = NULL;
32+
var $result_id = NULL;
33+
var $result_array = array();
34+
var $result_object = array();
35+
var $custom_result_object = array();
36+
var $current_row = 0;
37+
var $num_rows = 0;
38+
var $row_data = NULL;
3839

3940

4041
/**
@@ -46,11 +47,48 @@ class CI_DB_result {
4647
*/
4748
function result($type = 'object')
4849
{
49-
return ($type == 'object') ? $this->result_object() : $this->result_array();
50+
if ($type == 'array') return $this->result_array();
51+
else if ($type == 'object') return $this->result_object();
52+
else return $this->custom_result_object($type);
5053
}
5154

5255
// --------------------------------------------------------------------
5356

57+
/**
58+
* Custom query result.
59+
*
60+
* @param class_name A string that represents the type of object you want back
61+
* @return array of objects
62+
*/
63+
function custom_result_object($class_name)
64+
{
65+
if (array_key_exists($class_name, $this->custom_result_object))
66+
{
67+
return $this->custom_result_object[$class_name];
68+
}
69+
70+
if ($this->result_id === FALSE OR $this->num_rows() == 0)
71+
{
72+
return array();
73+
}
74+
75+
// add the data to the object
76+
$this->_data_seek(0);
77+
$result_object = array();
78+
while ($row = $this->_fetch_object())
79+
{
80+
$object = new $class_name();
81+
foreach($row as $key => $value)
82+
{
83+
$object->$key = $value;
84+
}
85+
$result_object[] = $object;
86+
}
87+
88+
// return the array
89+
return $this->custom_result_object[$class_name] = $result_object;
90+
}
91+
5492
/**
5593
* Query result. "object" version.
5694
*
@@ -142,7 +180,9 @@ function row($n = 0, $type = 'object')
142180
$n = 0;
143181
}
144182

145-
return ($type == 'object') ? $this->row_object($n) : $this->row_array($n);
183+
if ($type == 'object') return $this->row_object($n);
184+
else if ($type == 'array') return $this->row_array($n);
185+
else return $this->custom_row_object($n, $type);
146186
}
147187

148188
// --------------------------------------------------------------------
@@ -179,7 +219,30 @@ function set_row($key, $value = NULL)
179219

180220
// --------------------------------------------------------------------
181221

182-
/**
222+
/**
223+
* Returns a single result row - custom object version
224+
*
225+
* @access public
226+
* @return object
227+
*/
228+
function custom_row_object($n, $type)
229+
{
230+
$result = $this->custom_result_object($type);
231+
232+
if (count($result) == 0)
233+
{
234+
return $result;
235+
}
236+
237+
if ($n != $this->current_row AND isset($result[$n]))
238+
{
239+
$this->current_row = $n;
240+
}
241+
242+
return $result[$this->current_row];
243+
}
244+
245+
/**
183246
* Returns a single result row - object version
184247
*
185248
* @access public
@@ -339,4 +402,4 @@ function _fetch_object() { return array(); }
339402
// END DB_result class
340403

341404
/* End of file DB_result.php */
342-
/* Location: ./system/database/DB_result.php */
405+
/* Location: ./system/database/DB_result.php */

0 commit comments

Comments
 (0)