28
28
*/
29
29
class CI_DB_result {
30
30
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 ;
38
39
39
40
40
41
/**
@@ -46,11 +47,48 @@ class CI_DB_result {
46
47
*/
47
48
function result ($ type = 'object ' )
48
49
{
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 );
50
53
}
51
54
52
55
// --------------------------------------------------------------------
53
56
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
+
54
92
/**
55
93
* Query result. "object" version.
56
94
*
@@ -142,7 +180,9 @@ function row($n = 0, $type = 'object')
142
180
$ n = 0 ;
143
181
}
144
182
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 );
146
186
}
147
187
148
188
// --------------------------------------------------------------------
@@ -179,7 +219,30 @@ function set_row($key, $value = NULL)
179
219
180
220
// --------------------------------------------------------------------
181
221
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
+ /**
183
246
* Returns a single result row - object version
184
247
*
185
248
* @access public
@@ -339,4 +402,4 @@ function _fetch_object() { return array(); }
339
402
// END DB_result class
340
403
341
404
/* End of file DB_result.php */
342
- /* Location: ./system/database/DB_result.php */
405
+ /* Location: ./system/database/DB_result.php */
0 commit comments