Changeset 2268
- Timestamp:
- 03/10/2008 09:11:45 AM (9 months ago)
- Files:
-
- 1 modified
-
trunk/system/libraries/ORM.php (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/system/libraries/ORM.php
r2262 r2268 219 219 } 220 220 221 if (substr($method, 0, 8) === 'find_by_' OR ($all = substr($method, 0, 12)) === 'find_all_by_') 222 { 223 $method = isset($all) ? substr($method, 12) : substr($method, 8); 224 221 if (substr($method, 0, 8) === 'find_by_' OR substr($method, 0, 12) === 'find_all_by_') 222 { 225 223 // Make a find_by call 226 224 return $this->call_find_by($method, $args); … … 230 228 { 231 229 // Make a find_related call 232 return $this->call_find_related( substr($method, 13));230 return $this->call_find_related($method, $args); 233 231 } 234 232 235 233 if (preg_match('/^(has|add|remove)_(.+)/', $method, $matches)) 236 234 { 237 $action = $matches[1];238 $model = is_object(current($args)) ? current($args) : $this->load_model($matches[2]);239 240 235 // Make a has/add/remove call 241 return $this->call_has_add_remove($ action, $model);236 return $this->call_has_add_remove($method, $args, $matches); 242 237 } 243 238 … … 278 273 * __call: find_by_*, find_all_by_* 279 274 * 280 * @param string column names to find by275 * @param string method 281 276 * @param array arguments 282 277 * @return object … … 284 279 protected function call_find_by($method, $args) 285 280 { 281 // Use ALL 282 $ALL = (substr($method, 0, 12) === 'find_all_by_'); 283 284 // Method args 285 $method = $ALL ? substr($method, 12) : substr($method, 8); 286 286 287 // WHERE is manually set 287 288 $this->where = TRUE; … … 319 320 } 320 321 321 if ( isset($all))322 if ($ALL) 322 323 { 323 324 // Array of results … … 334 335 * __call: find_related_* 335 336 * 336 * @param string table name 337 * @param string method name 338 * @param array arguments 337 339 * @return object 338 340 */ 339 protected function call_find_related($table) 340 { 341 protected function call_find_related($method, $args) 342 { 343 // Extract table name 344 $table = substr($method, 13) 345 341 346 // Construct a new model 342 347 $model = $this->load_model($table); … … 388 393 * __call: has_*, add_*, remove_* 389 394 * 390 * @param string action: has, add, remove 391 * @param object model 395 * @param string method 396 * @param array arguments 397 * @param array action matches 392 398 * @return boolean 393 399 */ 394 protected function call_has_add_remove($action, $model) 395 { 400 protected function call_has_add_remove($method, $args, $matches) 401 { 402 $action = $matches[1]; 403 $model = is_object(current($args)) ? current($args) : $this->load_model($matches[2]); 404 396 405 // Real foreign table name 397 406 $table = $model->table_name; … … 907 916 public function __construct($class, $result) 908 917 { 918 // Class name 909 919 $this->class = $class; 920 921 // Database result 910 922 $this->result = $result; 911 923 } … … 936 948 public function range($start, $end) 937 949 { 950 // Array of objects 938 951 $array = array(); 939 952 … … 948 961 for ($i = $start; $i < $end; $i++) 949 962 { 963 // Insert each object in the range 950 964 $array[] = new $class($this->result->offsetGet($i)); 951 965 } … … 971 985 $class = $this->class; 972 986 973 return $this->result->offsetExists(0) ? new $class($this->result->offsetGet(0)) : FALSE;987 return ($row = $this->result->current()) ? new $class($row) : FALSE; 974 988 } 975 989
