| 1 |
<?php defined('SYSPATH') or die('No direct script access.'); |
|---|
| 2 |
|
|---|
| 3 |
* Object Relational Mapping (ORM) is a method of abstracting database |
|---|
| 4 |
* access to standard PHP calls. All table rows are represented as a model. |
|---|
| 5 |
* |
|---|
| 6 |
* @see http://en.wikipedia.org/wiki/Active_record |
|---|
| 7 |
* @see http://en.wikipedia.org/wiki/Object-relational_mapping |
|---|
| 8 |
* |
|---|
| 9 |
* $Id$ |
|---|
| 10 |
* |
|---|
| 11 |
* @package Core |
|---|
| 12 |
* @author Kohana Team |
|---|
| 13 |
* @copyright (c) 2007-2008 Kohana Team |
|---|
| 14 |
* @license http://kohanaphp.com/license.html |
|---|
| 15 |
*/ |
|---|
| 16 |
class ORM_Core { |
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
protected static $fields = array(); |
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
protected static $db; |
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
protected $auto_save = FALSE; |
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
protected $class; |
|---|
| 29 |
protected $table; |
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
protected $select = FALSE; |
|---|
| 33 |
protected $where = FALSE; |
|---|
| 34 |
protected $from = FALSE; |
|---|
| 35 |
protected $order = FALSE; |
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
protected $sort = array('id', 'asc'); |
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
protected $object; |
|---|
| 42 |
protected $object_related; |
|---|
| 43 |
protected $loaded = FALSE; |
|---|
| 44 |
protected $saved = FALSE; |
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
protected $changed = array(); |
|---|
| 48 |
protected $changed_related = array(); |
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
protected $has_one = array(); |
|---|
| 52 |
protected $has_many = array(); |
|---|
| 53 |
protected $belongs_to = array(); |
|---|
| 54 |
protected $belongs_to_many = array(); |
|---|
| 55 |
protected $has_and_belongs_to_many = array(); |
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
* Factory method. Creates an instance of an ORM model and returns it. |
|---|
| 59 |
* |
|---|
| 60 |
* @param string model name |
|---|
| 61 |
* @param mixed id to load |
|---|
| 62 |
* @return object |
|---|
| 63 |
*/ |
|---|
| 64 |
public static function factory($model = FALSE, $id = FALSE) |
|---|
| 65 |
{ |
|---|
| 66 |
$model = empty($model) ? __CLASS__ : ucfirst($model).'_Model'; |
|---|
| 67 |
return new $model($id); |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
* Initialize database, setup internal variables, find requested object. |
|---|
| 72 |
* |
|---|
| 73 |
* @return void |
|---|
| 74 |
*/ |
|---|
| 75 |
public function __construct($id = FALSE) |
|---|
| 76 |
{ |
|---|
| 77 |
|
|---|
| 78 |
empty($this->class) and $this->class = strtolower(substr(get_class($this), 0, -6)); |
|---|
| 79 |
empty($this->table) and $this->table = inflector::plural($this->class); |
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
$this->connect(); |
|---|
| 83 |
|
|---|
| 84 |
if (is_object($id)) |
|---|
| 85 |
{ |
|---|
| 86 |
|
|---|
| 87 |
$this->object = $id; |
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
$this->load_object_types(); |
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
$this->loaded = $this->saved = TRUE; |
|---|
| 94 |
} |
|---|
| 95 |
else |
|---|
| 96 |
{ |
|---|
| 97 |
if (empty($id)) |
|---|
| 98 |
{ |
|---|
| 99 |
|
|---|
| 100 |
$this->clear(); |
|---|
| 101 |
} |
|---|
| 102 |
else |
|---|
| 103 |
{ |
|---|
| 104 |
|
|---|
| 105 |
$this->find($id); |
|---|
| 106 |
} |
|---|
| 107 |
} |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
* Enables automatic saving of the object when the model is destroyed. |
|---|
| 112 |
* |
|---|
| 113 |
* @return void |
|---|
| 114 |
*/ |
|---|
| 115 |
public function __destruct() |
|---|
| 116 |
{ |
|---|
| 117 |
if ($this->auto_save == TRUE) |
|---|
| 118 |
{ |
|---|
| 119 |
try |
|---|
| 120 |
{ |
|---|
| 121 |
|
|---|
| 122 |
$this->save(); |
|---|
| 123 |
} |
|---|
| 124 |
catch (Exception $e) |
|---|
| 125 |
{ |
|---|
| 126 |
|
|---|
| 127 |
// "stack frame" errors. http://bugs.php.net/bug.php?id=33598 |
|---|
| 128 |
Log::add('error', $e->getMessage()); |
|---|
| 129 |
} |
|---|
| 130 |
} |
|---|
| 131 |
} |
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
* Reloads the database when the object is unserialized. |
|---|
| 135 |
* |
|---|
| 136 |
* @return void |
|---|
| 137 |
*/ |
|---|
| 138 |
public function __wakeup() |
|---|
| 139 |
{ |
|---|
| 140 |
|
|---|
| 141 |
$this->connect(); |
|---|
| 142 |
} |
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
* Magic method for getting object and model keys. |
|---|
| 146 |
* |
|---|
| 147 |
* @param string key name |
|---|
| 148 |
* @return mixed |
|---|
| 149 |
*/ |
|---|
| 150 |
public function __get($key) |
|---|
| 151 |
{ |
|---|
| 152 |
if (isset($this->object->$key)) |
|---|
| 153 |
{ |
|---|
| 154 |
return $this->object->$key; |
|---|
| 155 |
} |
|---|
| 156 |
elseif (in_array($key, $this->has_one) OR in_array($key, $this->belongs_to)) |
|---|
| 157 |
{ |
|---|
| 158 |
|
|---|
| 159 |
$model = ucfirst($key).'_Model'; |
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
$child_id = $key.'_id'; |
|---|
| 163 |
|
|---|
| 164 |
$this->object->$key = new $model |
|---|
| 165 |
( |
|---|
| 166 |
isset($this->object->$child_id) |
|---|
| 167 |
|
|---|
| 168 |
? $this->object->$child_id |
|---|
| 169 |
|
|---|
| 170 |
: array($this->class.'_id' => $this->object->id) |
|---|
| 171 |
); |
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
return $this->object->$key; |
|---|
| 175 |
} |
|---|
| 176 |
elseif (in_array($key, $this->has_and_belongs_to_many)) |
|---|
| 177 |
{ |
|---|
| 178 |
if (empty($this->object_related[$key]['cur'])) |
|---|
| 179 |
{ |
|---|
| 180 |
|
|---|
| 181 |
$this->object_related[$key]['cur'] = array(); |
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
$fk = inflector::singular($key).'_id'; |
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 |
$query = self::$db |
|---|
| 188 |
->select($fk) |
|---|
| 189 |
->from($this->related_table($key)) |
|---|
| 190 |
->where($this->class.'_id', $this->id) |
|---|
| 191 |
->get(); |
|---|
| 192 |
|
|---|
| 193 |
foreach ($query as $row) |
|---|
| 194 |
{ |
|---|
| 195 |
|
|---|
| 196 |
$this->object_related[$key]['cur'][] = $row->$fk; |
|---|
| 197 |
} |
|---|
| 198 |
} |
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 |
return $this->object_related[$key]['cur']; |
|---|
| 202 |
} |
|---|
| 203 |
else |
|---|
| 204 |
{ |
|---|
| 205 |
switch ($key) |
|---|
| 206 |
{ |
|---|
| 207 |
case 'table_name': |
|---|
| 208 |
return $this->table; |
|---|
| 209 |
break; |
|---|
| 210 |
case 'class_name': |
|---|
| 211 |
return $this->class; |
|---|
| 212 |
break; |
|---|
| 213 |
case 'auto_save': |
|---|
| 214 |
return $this->auto_save; |
|---|
| 215 |
break; |
|---|
| 216 |
} |
|---|
| 217 |
} |
|---|
| 218 |
} |
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 |
* Magic method for setting object and model keys. |
|---|
| 222 |
* |
|---|
| 223 |
* @param string key name |
|---|
| 224 |
* @param mixed value to set |
|---|
| 225 |
* @return void |
|---|
| 226 |
*/ |
|---|
| 227 |
public function __set($key, $value) |
|---|
| 228 |
{ |
|---|
| 229 |
if ($key != 'id' AND isset(self::$fields[$this->table][$key])) |
|---|
| 230 |
{ |
|---|
| 231 |
|
|---|
| 232 |
$value = $this->set_value_type($key, $value); |
|---|
| 233 |
|
|---|
| 234 |
if ($this->object->$key !== $value) |
|---|
| 235 |
{ |
|---|
| 236 |
|
|---|
| 237 |
$this->object->$key = $value; |
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
$this->changed[$key] = $key; |
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
$this->saved = FALSE; |
|---|
| 244 |
} |
|---|
| 245 |
} |
|---|
| 246 |
elseif (in_array($key, $this->has_and_belongs_to_many) AND is_array($value)) |
|---|
| 247 |
{ |
|---|
| 248 |
if (empty($this->object_related[$key])) |
|---|
| 249 |
{ |
|---|
| 250 |
|
|---|
| 251 |
$this->$key; |
|---|
| 252 |
} |
|---|
| 253 |
|
|---|
| 254 |
|
|---|
| 255 |
$this->object_related[$key]['new'] = $value; |
|---|
| 256 |
|
|---|
| 257 |
|
|---|
| 258 |
$this->changed_related[$key] = $key; |
|---|
| 259 |
} |
|---|
| 260 |
else |
|---|
| 261 |
{ |
|---|
| 262 |
switch ($key) |
|---|
| 263 |
{ |
|---|
| 264 |
case 'auto_save': |
|---|
| 265 |
$this->auto_save = (bool) $value; |
|---|
| 266 |
break; |
|---|
| 267 |
} |
|---|
| 268 |
} |
|---|
| 269 |
} |
|---|
| 270 |
|
|---|
| 271 |
|
|---|
| 272 |
* Magic method for calling ORM methods. This handles: |
|---|
| 273 |
* - as_array |
|---|
| 274 |
* - find_by_* |
|---|
| 275 |
* - find_all_by_* |
|---|
| 276 |
* - find_related_* |
|---|
| 277 |
* - has_* |
|---|
| 278 |
* - add_* |
|---|
| 279 |
* - remove_* |
|---|
| 280 |
* |
|---|
| 281 |
* @throws Kohana_Exception |
|---|
| 282 |
* @param string method name |
|---|
| 283 |
* @param array method arguments |
|---|
| 284 |
* @return mixed |
|---|
| 285 |
*/ |
|---|
| 286 |
public function __call($method, $args) |
|---|
| 287 |
{ |
|---|
| 288 |
if ($method === 'as_array') |
|---|
| 289 |
{ |
|---|
| 290 |
|
|---|
| 291 |
return (array) $this->object; |
|---|
| 292 |
} |
|---|
| 293 |
|
|---|
| 294 |
if (substr($method, 0, 8) === 'find_by_' OR substr($method, 0, 12) === 'find_all_by_') |
|---|
| 295 |
{ |
|---|
| 296 |
|
|---|
| 297 |
return $this->call_find_by($method, $args); |
|---|
| 298 |
} |
|---|
| 299 |
|
|---|
| 300 |
if (substr($method, 0, 13) === 'find_related_') |
|---|
| 301 |
{ |
|---|
| 302 |
|
|---|
| 303 |
return $this->call_find_related(substr($method, 13), $args); |
|---|
| 304 |
} |
|---|
| 305 |
|
|---|
| 306 |
if (preg_match('/^(has|add|remove)_(.+)$/', $method, $matches)) |
|---|
| 307 |
{ |
|---|
| 308 |
if (empty($this->object->id)) |
|---|
| 309 |
{ |
|---|
| 310 |
|
|---|
| 311 |
return FALSE; |
|---|
| 312 |
} |
|---|
| 313 |
|
|---|
| 314 |
|
|---|
| 315 |
return $this->call_has_add_remove($method, $args, $matches); |
|---|
| 316 |
} |
|---|
| 317 |
|
|---|
| 318 |
if (method_exists(self::$db, $method)) |
|---|
| 319 |
{ |
|---|
| 320 |
|
|---|
| 321 |
if (preg_match('/^(?:query|get|insert|update|list_fields|field_data)$/', $method)) |
|---|
| 322 |
return $this; |
|---|
| 323 |
|
|---|
| 324 |
if ($method === 'select') |
|---|
| 325 |
{ |
|---|
| 326 |
$this->select = TRUE; |
|---|
| 327 |
} |
|---|
| 328 |
elseif (preg_match('/where|like|in|regex/', $method)) |
|---|
| 329 |
{ |
|---|
| 330 |
$this->where = TRUE; |
|---|
| 331 |
} |
|---|
| 332 |
elseif ($method === 'from') |
|---|
| 333 |
{ |
|---|
| 334 |
$this->from = TRUE; |
|---|
| 335 |
} |
|---|
| 336 |
elseif ($method === 'orderby') |
|---|
| 337 |
{ |
|---|
| 338 |
$this->order = TRUE; |
|---|
| 339 |
} |
|---|
| 340 |
|
|---|
| 341 |
|
|---|
| 342 |
switch (count($args)) |
|---|
| 343 |
{ |
|---|
| 344 |
case 0: |
|---|
| 345 |
return self::$db->$method(); |
|---|
| 346 |
break; |
|---|
| 347 |
case 1: |
|---|
| 348 |
self::$db->$method(current($args)); |
|---|
| 349 |
break; |
|---|
| 350 |
case 2: |
|---|
| 351 |
self::$db->$method(current($args), next($args)); |
|---|
| 352 |
break; |
|---|
| 353 |
default: |
|---|
| 354 |
call_user_func_array(array(self::$db, $method), $args); |
|---|
| 355 |
break; |
|---|
| 356 |
} |
|---|
| 357 |
|
|---|
| 358 |
return $this; |
|---|
| 359 |
} |
|---|
| 360 |
|
|---|
| 361 |
|
|---|
| 362 |
throw new Kohana_Exception('orm.method_not_implemented', $method, get_class($this)); |
|---|
| 363 |
} |
|---|
| 364 |
|
|---|
| 365 |
|
|---|
| 366 |
* __call: find_by_*, find_all_by_* |
|---|
| 367 |
* |
|---|
| 368 |
* @param string method |
|---|
| 369 |
* @param array arguments |
|---|
| 370 |
* @return object |
|---|
| 371 |
*/ |
|---|
| 372 |
protected function call_find_by($method, $args) |
|---|
| 373 |
{ |
|---|
| 374 |
|
|---|
| 375 |
$ALL = (substr($method, 0, 12) === 'find_all_by_'); |
|---|
| 376 |
|
|---|
| 377 |
|
|---|
| 378 |
$method = $ALL ? substr($method, 12) : substr($method, 8); |
|---|
| 379 |
|
|---|
| 380 |
|
|---|
| 381 |
$this->where = TRUE; |
|---|
| 382 |
|
|---|
| 383 |
|
|---|
| 384 |
if (is_array($keys = $this->find_keys($method))) |
|---|
| 385 |
{ |
|---|
| 386 |
if (strpos($method, '_or_') === FALSE) |
|---|
| 387 |
{ |
|---|
| 388 |
|
|---|
| 389 |
self::$db->where(array_combine($keys, $args)); |
|---|
| 390 |
} |
|---|
| 391 |
else |
|---|
| 392 |
{ |
|---|
| 393 |
if (count($args) === 1) |
|---|
| 394 |
{ |
|---|
| 395 |
$val = current($args); |
|---|
| 396 |
foreach ($keys as $key) |
|---|
| 397 |
{ |
|---|
| 398 |
|
|---|
| 399 |
self::$db->orwhere(array($key => $val)); |
|---|
| 400 |
} |
|---|
| 401 |
} |
|---|
| 402 |
else |
|---|
| 403 |
{ |
|---|
| 404 |
|
|---|
| 405 |
self::$db->orwhere(array_combine($keys, $args)); |
|---|
| 406 |
} |
|---|
| 407 |
} |
|---|
| 408 |
} |
|---|
| 409 |
else |
|---|
| 410 |
{ |
|---|
| 411 |
|
|---|
| 412 |
self::$db->where(array($keys => current($args))); |
|---|
| 413 |
} |
|---|
| 414 |
|
|---|
| 415 |
if ($ALL) |
|---|
| 416 |
{ |
|---|
| 417 |
|
|---|
| 418 |
return $this->load_result(TRUE); |
|---|
| 419 |
} |
|---|
| 420 |
else |
|---|
| 421 |
{ |
|---|
| 422 |
|
|---|
| 423 |
return $this->find(); |
|---|
| 424 |
} |
|---|
| 425 |
} |
|---|
| 426 |
|
|---|
| 427 |
|
|---|
| 428 |
* __call: find_related_* |
|---|
| 429 |
* |
|---|
| 430 |
* @param string method name |
|---|
| 431 |
* @param array arguments |
|---|
| 432 |
* @return object |
|---|
| 433 |
*/ |
|---|
| 434 |
protected function call_find_related($table, $args) |
|---|
| 435 |
{ |
|---|
| 436 |
|
|---|
| 437 |
$model = $this->load_model($table); |
|---|
| 438 |
|
|---|
| 439 |
|
|---|
| 440 |
$remote = array($this->class.'_id' => $this->object->id); |
|---|
| 441 |
|
|---|
| 442 |
if (in_array($table, $this->has_one)) |
|---|
| 443 |
{ |
|---|
| 444 |
|
|---|
| 445 |
return $model->where($remote)->find(); |
|---|
| 446 |
} |
|---|
| 447 |
elseif (in_array($table, $this->has_many)) |
|---|
| 448 |
{ |
|---|
| 449 |
|
|---|
| 450 |
$model->where($remote); |
|---|
| 451 |
} |
|---|
| 452 |
elseif (in_array($table, $this->has_and_belongs_to_many)) |
|---|
| 453 |
{ |
|---|
| 454 |
|
|---|
| 455 |
$this->related_join($table); |
|---|
| 456 |
} |
|---|
| 457 |
elseif (in_array($table, $this->belongs_to_many)) |
|---|
| 458 |
{ |
|---|
| 459 |
|
|---|
| 460 |
$id = $this->class.'_id'; |
|---|
| 461 |
|
|---|
| 462 |
if ($model->$id === NULL) |
|---|
| 463 |
{ |
|---|
| 464 |
|
|---|
| 465 |
$this->related_join($table); |
|---|
| 466 |
} |
|---|
| 467 |
else |
|---|
| 468 |
{ |
|---|
| 469 |
|
|---|
| 470 |
$model->where($remote); |
|---|
| 471 |
} |
|---|
| 472 |
} |
|---|
| 473 |
else |
|---|
| 474 |
{ |
|---|
| 475 |
|
|---|
| 476 |
return FALSE; |
|---|
| 477 |
} |
|---|
| 478 |
|
|---|
| 479 |
return $model->load_result(TRUE); |
|---|
| 480 |
} |
|---|
| 481 |
|
|---|
| 482 |
|
|---|
| 483 |
* __call: has_*, add_*, remove_* |
|---|
| 484 |
* |
|---|
| 485 |
* @param string method |
|---|
| 486 |
* @param array arguments |
|---|
| 487 |
* @param array action matches |
|---|
| 488 |
* @return boolean |
|---|
| 489 |
*/ |
|---|
| 490 |
protected function call_has_add_remove($method, $args, $matches) |
|---|
| 491 |
{ |
|---|
| 492 |
$action = $matches[1]; |
|---|
| 493 |
$model = (count($args) > 0 AND is_object($args[0])) ? $args[0] : $this->load_model($matches[2]); |
|---|
| 494 |
|
|---|
| 495 |
|
|---|
| 496 |
$table = $model->table_name; |
|---|
| 497 |
|
|---|
| 498 |
|
|---|
| 499 |
if (in_array($matches[2], $this->has_one)) |
|---|
| 500 |
{ |
|---|
| 501 |
$ownership = 1; |
|---|
| 502 |
} |
|---|
| 503 |
elseif (in_array($table, $this->has_many)) |
|---|
| 504 |
{ |
|---|
| 505 |
$ownership = 2; |
|---|
| 506 |
} |
|---|
| 507 |
elseif (in_array($table, $this->has_and_belongs_to_many)) |
|---|
| 508 |
{ |
|---|
| 509 |
$ownership = 3; |
|---|
| 510 |
} |
|---|
| 511 |
else |
|---|
| 512 |
{ |
|---|
| 513 |
|
|---|
| 514 |
return FALSE; |
|---|
| 515 |
} |
|---|
| 516 |
|
|---|
| 517 |
|
|---|
| 518 |
$primary = $this->class.'_id'; |
|---|
| 519 |
|
|---|
| 520 |
|
|---|
| 521 |
$foreign = $model->class_name.'_id'; |
|---|
| 522 |
|
|---|
| 523 |
if ( ! is_object(current($args))) |
|---|
| 524 |
{ |
|---|
| 525 |
if ($action === 'add' AND is_array(current($args))) |
|---|
| 526 |
{ |
|---|
| 527 |
$arg = current($args); |
|---|
| 528 |
|
|---|
| 529 |
foreach ($arg as $key => $val) |
|---|
| 530 |
{ |
|---|
| 531 |
|
|---|
| 532 |
$model->$key = $val; |
|---|
| 533 |
} |
|---|
| 534 |
} |
|---|
| 535 |
else |
|---|
| 536 |
{ |
|---|
| 537 |
if ($ownership === 1 OR $ownership === 2) |
|---|
| 538 |
{ |
|---|
| 539 |
|
|---|
| 540 |
self::$db->where($primary, $this->object->id); |
|---|
| 541 |
} |
|---|
| 542 |
|
|---|
| 543 |
|
|---|
| 544 |
$model->find(current($args)); |
|---|
| 545 |
} |
|---|
| 546 |
} |
|---|
| 547 |
|
|---|
| 548 |
if ($ownership === 3) |
|---|
| 549 |
{ |
|---|
| 550 |
|
|---|
| 551 |
$model->save(); |
|---|
| 552 |
|
|---|
| 553 |
|
|---|
| 554 |
$relationship = array |
|---|
| 555 |
( |
|---|
| 556 |
$primary => $this->object->id, |
|---|
| 557 |
$foreign => $model |
|---|