| Total Complexity | 123 |
| Total Lines | 943 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Model often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Model, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class Model{ |
||
| 37 | |||
| 38 | /* -------------------------------------------------------------- |
||
| 39 | * VARIABLES |
||
| 40 | * ------------------------------------------------------------ */ |
||
| 41 | |||
| 42 | /** |
||
| 43 | * This model's default database table. Automatically |
||
| 44 | * guessed by pluralising the model name. |
||
| 45 | */ |
||
| 46 | protected $_table; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The database connection object. Will be set to the default |
||
| 50 | * connection. This allows individual models to use different DBs |
||
| 51 | * without overwriting the global database connection. |
||
| 52 | */ |
||
| 53 | protected $_database; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * This model's default primary key or unique identifier. |
||
| 57 | * Used by the get(), update() and delete() functions. |
||
| 58 | */ |
||
| 59 | protected $primary_key = 'id'; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Support for soft deletes and this model's 'deleted' key |
||
| 63 | */ |
||
| 64 | protected $soft_delete = false; |
||
| 65 | protected $soft_delete_key = 'is_deleted'; |
||
| 66 | protected $_temporary_with_deleted = FALSE; |
||
| 67 | protected $_temporary_only_deleted = FALSE; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * The various callbacks available to the model. Each are |
||
| 71 | * simple lists of method names (methods will be run on $this). |
||
| 72 | */ |
||
| 73 | protected $before_create = array(); |
||
| 74 | protected $after_create = array(); |
||
| 75 | protected $before_update = array(); |
||
| 76 | protected $after_update = array(); |
||
| 77 | protected $before_get = array(); |
||
| 78 | protected $after_get = array(); |
||
| 79 | protected $before_delete = array(); |
||
| 80 | protected $after_delete = array(); |
||
| 81 | |||
| 82 | protected $callback_parameters = array(); |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Protected, non-modifiable attributes |
||
| 86 | */ |
||
| 87 | protected $protected_attributes = array(); |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Relationship arrays. Use flat strings for defaults or string |
||
| 91 | * => array to customise the class name and primary key |
||
| 92 | */ |
||
| 93 | protected $belongs_to = array(); |
||
| 94 | protected $has_many = array(); |
||
| 95 | |||
| 96 | protected $_with = array(); |
||
| 97 | |||
| 98 | /** |
||
| 99 | * An array of validation rules. This needs to be the same format |
||
| 100 | * as validation rules passed to the FormValidation library. |
||
| 101 | */ |
||
| 102 | protected $validate = array(); |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Optionally skip the validation. Used in conjunction with |
||
| 106 | * skip_validation() to skip data validation for any future calls. |
||
| 107 | */ |
||
| 108 | protected $skip_validation = FALSE; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * By default we return our results as objects. If we need to override |
||
| 112 | * this, we can, or, we could use the `as_array()` and `as_object()` scopes. |
||
| 113 | */ |
||
| 114 | protected $return_type = 'object'; |
||
| 115 | protected $_temporary_return_type = NULL; |
||
| 116 | |||
| 117 | |||
| 118 | /** |
||
| 119 | The database cache time |
||
| 120 | */ |
||
| 121 | protected $dbCacheTime = 0; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Instance of the Loader class |
||
| 125 | * @var Loader |
||
| 126 | */ |
||
| 127 | protected $loaderInstance = null; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Instance of the FormValidation library |
||
| 131 | * @var FormValidation |
||
| 132 | */ |
||
| 133 | protected $formValidationInstance = null; |
||
| 134 | |||
| 135 | /* -------------------------------------------------------------- |
||
| 136 | * GENERIC METHODS |
||
| 137 | * ------------------------------------------------------------ */ |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Initialise the model, tie into the CodeIgniter superobject and |
||
| 141 | * try our best to guess the table name. |
||
| 142 | */ |
||
| 143 | public function __construct(Database $db = null){ |
||
| 144 | if(is_object($db)){ |
||
| 145 | $this->setDatabaseInstance($db); |
||
| 146 | } |
||
| 147 | else{ |
||
| 148 | $obj = & get_instance(); |
||
| 149 | if(isset($obj->database) && is_object($obj->database)){ |
||
| 150 | /** |
||
| 151 | * NOTE: Need use "clone" because some Model need have the personal instance of the database library |
||
| 152 | * to prevent duplication |
||
| 153 | */ |
||
| 154 | $this->setDatabaseInstance(clone $obj->database); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | array_unshift($this->before_create, 'protect_attributes'); |
||
| 159 | array_unshift($this->before_update, 'protect_attributes'); |
||
| 160 | $this->_temporary_return_type = $this->return_type; |
||
| 161 | } |
||
| 162 | |||
| 163 | /* -------------------------------------------------------------- |
||
| 164 | * CRUD INTERFACE |
||
| 165 | * ------------------------------------------------------------ */ |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Fetch a single record based on the primary key. Returns an object. |
||
| 169 | */ |
||
| 170 | public function get($primary_value) |
||
| 171 | { |
||
| 172 | return $this->get_by($this->primary_key, $primary_value); |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Fetch a single record based on an arbitrary WHERE call. Can be |
||
| 177 | * any valid value to $this->_database->where(). |
||
| 178 | */ |
||
| 179 | public function get_by() |
||
| 180 | { |
||
| 181 | $where = func_get_args(); |
||
| 182 | |||
| 183 | if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
||
| 184 | { |
||
| 185 | $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
||
| 186 | } |
||
| 187 | |||
| 188 | $this->_set_where($where); |
||
| 189 | |||
| 190 | $this->trigger('before_get'); |
||
| 191 | $type = $this->_temporary_return_type == 'array' ? 'array' : false; |
||
|
|
|||
| 192 | $row = $this->_database->from($this->_table)->get($type); |
||
| 193 | $this->_temporary_return_type = $this->return_type; |
||
| 194 | $row = $this->trigger('after_get', $row); |
||
| 195 | $this->_with = array(); |
||
| 196 | return $row; |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Fetch an array of records based on an array of primary values. |
||
| 201 | */ |
||
| 202 | public function get_many($values) |
||
| 203 | { |
||
| 204 | $this->_database->in($this->primary_key, $values); |
||
| 205 | return $this->get_all(); |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Fetch an array of records based on an arbitrary WHERE call. |
||
| 210 | */ |
||
| 211 | public function get_many_by() |
||
| 212 | { |
||
| 213 | $where = func_get_args(); |
||
| 214 | $this->_set_where($where); |
||
| 215 | return $this->get_all(); |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Fetch all the records in the table. Can be used as a generic call |
||
| 220 | * to $this->_database->get() with scoped methods. |
||
| 221 | */ |
||
| 222 | public function get_all() |
||
| 223 | { |
||
| 224 | $this->trigger('before_get'); |
||
| 225 | if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
||
| 226 | { |
||
| 227 | $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
||
| 228 | } |
||
| 229 | $type = $this->_temporary_return_type == 'array' ? 'array':false; |
||
| 230 | $result = $this->_database->from($this->_table)->getAll($type); |
||
| 231 | $this->_temporary_return_type = $this->return_type; |
||
| 232 | |||
| 233 | foreach ($result as $key => &$row) |
||
| 234 | { |
||
| 235 | $row = $this->trigger('after_get', $row, ($key == count($result) - 1)); |
||
| 236 | } |
||
| 237 | $this->_with = array(); |
||
| 238 | return $result; |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Insert a new row into the table. $data should be an associative array |
||
| 243 | * of data to be inserted. Returns newly created ID. |
||
| 244 | */ |
||
| 245 | public function insert($data = array(), $skip_validation = FALSE, $escape = true) |
||
| 246 | { |
||
| 247 | if ($skip_validation === FALSE) |
||
| 248 | { |
||
| 249 | $data = $this->validate($data); |
||
| 250 | } |
||
| 251 | |||
| 252 | if ($data !== FALSE) |
||
| 253 | { |
||
| 254 | $data = $this->trigger('before_create', $data); |
||
| 255 | $this->_database->from($this->_table)->insert($data, $escape); |
||
| 256 | $insert_id = $this->_database->insertId(); |
||
| 257 | $this->trigger('after_create', $insert_id); |
||
| 258 | return $insert_id; |
||
| 259 | } |
||
| 260 | else |
||
| 261 | { |
||
| 262 | return FALSE; |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Insert multiple rows into the table. Returns an array of multiple IDs. |
||
| 268 | */ |
||
| 269 | public function insert_many($data = array(), $skip_validation = FALSE, $escape = true) |
||
| 270 | { |
||
| 271 | $ids = array(); |
||
| 272 | foreach ($data as $key => $row) |
||
| 273 | { |
||
| 274 | $ids[] = $this->insert($row, $skip_validation, $escape); |
||
| 275 | } |
||
| 276 | return $ids; |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Updated a record based on the primary value. |
||
| 281 | */ |
||
| 282 | public function update($primary_value, $data = array(), $skip_validation = FALSE, $escape = true) |
||
| 283 | { |
||
| 284 | $data = $this->trigger('before_update', $data); |
||
| 285 | if ($skip_validation === FALSE) |
||
| 286 | { |
||
| 287 | $data = $this->validate($data); |
||
| 288 | } |
||
| 289 | |||
| 290 | if ($data !== FALSE) |
||
| 291 | { |
||
| 292 | $result = $this->_database->where($this->primary_key, $primary_value) |
||
| 293 | ->from($this->_table) |
||
| 294 | ->update($data, $escape); |
||
| 295 | $this->trigger('after_update', array($data, $result)); |
||
| 296 | return $result; |
||
| 297 | } |
||
| 298 | else |
||
| 299 | { |
||
| 300 | return FALSE; |
||
| 301 | } |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Update many records, based on an array of primary values. |
||
| 306 | */ |
||
| 307 | public function update_many($primary_values, $data = array(), $skip_validation = FALSE, $escape = true) |
||
| 308 | { |
||
| 309 | $data = $this->trigger('before_update', $data); |
||
| 310 | if ($skip_validation === FALSE) |
||
| 311 | { |
||
| 312 | $data = $this->validate($data); |
||
| 313 | } |
||
| 314 | if ($data !== FALSE) |
||
| 315 | { |
||
| 316 | $result = $this->_database->in($this->primary_key, $primary_values) |
||
| 317 | ->from($this->_table) |
||
| 318 | ->update($data, $escape); |
||
| 319 | $this->trigger('after_update', array($data, $result)); |
||
| 320 | return $result; |
||
| 321 | } |
||
| 322 | else |
||
| 323 | { |
||
| 324 | return FALSE; |
||
| 325 | } |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Updated a record based on an arbitrary WHERE clause. |
||
| 330 | */ |
||
| 331 | public function update_by() |
||
| 332 | { |
||
| 333 | $args = func_get_args(); |
||
| 334 | $data = array(); |
||
| 335 | if(count($args) == 2){ |
||
| 336 | if(is_array($args[1])){ |
||
| 337 | $data = array_pop($args); |
||
| 338 | } |
||
| 339 | } |
||
| 340 | else if(count($args) == 3){ |
||
| 341 | if(is_array($args[2])){ |
||
| 342 | $data = array_pop($args); |
||
| 343 | } |
||
| 344 | } |
||
| 345 | $data = $this->trigger('before_update', $data); |
||
| 346 | if ($this->validate($data) !== FALSE) |
||
| 347 | { |
||
| 348 | $this->_set_where($args); |
||
| 349 | $result = $this->_database->from($this->_table)->update($data); |
||
| 350 | $this->trigger('after_update', array($data, $result)); |
||
| 351 | return $result; |
||
| 352 | } |
||
| 353 | else |
||
| 354 | { |
||
| 355 | return FALSE; |
||
| 356 | } |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Update all records |
||
| 361 | */ |
||
| 362 | public function update_all($data = array(), $escape = true) |
||
| 363 | { |
||
| 364 | $data = $this->trigger('before_update', $data); |
||
| 365 | $result = $this->_database->from($this->_table)->update($data, $escape); |
||
| 366 | $this->trigger('after_update', array($data, $result)); |
||
| 367 | return $result; |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Delete a row from the table by the primary value |
||
| 372 | */ |
||
| 373 | public function delete($id) |
||
| 374 | { |
||
| 375 | $this->trigger('before_delete', $id); |
||
| 376 | $this->_database->where($this->primary_key, $id); |
||
| 377 | if ($this->soft_delete) |
||
| 378 | { |
||
| 379 | $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
||
| 380 | } |
||
| 381 | else |
||
| 382 | { |
||
| 383 | $result = $this->_database->from($this->_table)->delete(); |
||
| 384 | } |
||
| 385 | |||
| 386 | $this->trigger('after_delete', $result); |
||
| 387 | return $result; |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Delete a row from the database table by an arbitrary WHERE clause |
||
| 392 | */ |
||
| 393 | public function delete_by() |
||
| 394 | { |
||
| 395 | $where = func_get_args(); |
||
| 396 | $where = $this->trigger('before_delete', $where); |
||
| 397 | $this->_set_where($where); |
||
| 398 | if ($this->soft_delete) |
||
| 399 | { |
||
| 400 | $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
||
| 401 | } |
||
| 402 | else |
||
| 403 | { |
||
| 404 | $result = $this->_database->from($this->_table)->delete(); |
||
| 405 | } |
||
| 406 | $this->trigger('after_delete', $result); |
||
| 407 | return $result; |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Delete many rows from the database table by multiple primary values |
||
| 412 | */ |
||
| 413 | public function delete_many($primary_values) |
||
| 414 | { |
||
| 415 | $primary_values = $this->trigger('before_delete', $primary_values); |
||
| 416 | $this->_database->in($this->primary_key, $primary_values); |
||
| 417 | if ($this->soft_delete) |
||
| 418 | { |
||
| 419 | $result = $this->_database->from($this->_table)->update(array( $this->soft_delete_key => TRUE )); |
||
| 420 | } |
||
| 421 | else |
||
| 422 | { |
||
| 423 | $result = $this->_database->from($this->_table)->delete(); |
||
| 424 | } |
||
| 425 | $this->trigger('after_delete', $result); |
||
| 426 | return $result; |
||
| 427 | } |
||
| 428 | |||
| 429 | |||
| 430 | /** |
||
| 431 | * Truncates the table |
||
| 432 | */ |
||
| 433 | public function truncate() |
||
| 434 | { |
||
| 435 | $result = $this->_database->from($this->_table)->delete(); |
||
| 436 | return $result; |
||
| 437 | } |
||
| 438 | |||
| 439 | /* -------------------------------------------------------------- |
||
| 440 | * RELATIONSHIPS |
||
| 441 | * ------------------------------------------------------------ */ |
||
| 442 | |||
| 443 | public function with($relationship) |
||
| 444 | { |
||
| 445 | $this->_with[] = $relationship; |
||
| 446 | if (!in_array('relate', $this->after_get)) |
||
| 447 | { |
||
| 448 | $this->after_get[] = 'relate'; |
||
| 449 | } |
||
| 450 | return $this; |
||
| 451 | } |
||
| 452 | |||
| 453 | public function relate($row) |
||
| 454 | { |
||
| 455 | if (empty($row)) |
||
| 456 | { |
||
| 457 | return $row; |
||
| 458 | } |
||
| 459 | |||
| 460 | foreach ($this->belongs_to as $key => $value) |
||
| 461 | { |
||
| 462 | if (is_string($value)) |
||
| 463 | { |
||
| 464 | $relationship = $value; |
||
| 465 | $options = array( 'primary_key' => $value . '_id', 'model' => $value . '_model' ); |
||
| 466 | } |
||
| 467 | else |
||
| 468 | { |
||
| 469 | $relationship = $key; |
||
| 470 | $options = $value; |
||
| 471 | } |
||
| 472 | |||
| 473 | if (in_array($relationship, $this->_with)) |
||
| 474 | { |
||
| 475 | if(is_object($this->loaderInstance)){ |
||
| 476 | $this->loaderInstance->model($options['model'], $relationship . '_model'); |
||
| 477 | } |
||
| 478 | else{ |
||
| 479 | Loader::model($options['model'], $relationship . '_model'); |
||
| 480 | } |
||
| 481 | if (is_object($row)) |
||
| 482 | { |
||
| 483 | $row->{$relationship} = $this->{$relationship . '_model'}->get($row->{$options['primary_key']}); |
||
| 484 | } |
||
| 485 | else |
||
| 486 | { |
||
| 487 | $row[$relationship] = $this->{$relationship . '_model'}->get($row[$options['primary_key']]); |
||
| 488 | } |
||
| 489 | } |
||
| 490 | } |
||
| 491 | |||
| 492 | foreach ($this->has_many as $key => $value) |
||
| 493 | { |
||
| 494 | if (is_string($value)) |
||
| 495 | { |
||
| 496 | $relationship = $value; |
||
| 497 | $options = array( 'primary_key' => $this->_table . '_id', 'model' => $value . '_model' ); |
||
| 498 | } |
||
| 499 | else |
||
| 500 | { |
||
| 501 | $relationship = $key; |
||
| 502 | $options = $value; |
||
| 503 | } |
||
| 504 | |||
| 505 | if (in_array($relationship, $this->_with)) |
||
| 506 | { |
||
| 507 | if(is_object($this->loaderInstance)){ |
||
| 508 | $this->loaderInstance->model($options['model'], $relationship . '_model'); |
||
| 509 | } |
||
| 510 | else{ |
||
| 511 | Loader::model($options['model'], $relationship . '_model'); |
||
| 512 | } |
||
| 513 | if (is_object($row)) |
||
| 514 | { |
||
| 515 | $row->{$relationship} = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row->{$this->primary_key}); |
||
| 516 | } |
||
| 517 | else |
||
| 518 | { |
||
| 519 | $row[$relationship] = $this->{$relationship . '_model'}->get_many_by($options['primary_key'], $row[$this->primary_key]); |
||
| 520 | } |
||
| 521 | } |
||
| 522 | } |
||
| 523 | return $row; |
||
| 524 | } |
||
| 525 | |||
| 526 | /* -------------------------------------------------------------- |
||
| 527 | * UTILITY METHODS |
||
| 528 | * ------------------------------------------------------------ */ |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Retrieve and generate a form_dropdown friendly array |
||
| 532 | */ |
||
| 533 | public function dropdown() |
||
| 534 | { |
||
| 535 | $args = func_get_args(); |
||
| 536 | if(count($args) == 2) |
||
| 537 | { |
||
| 538 | list($key, $value) = $args; |
||
| 539 | } |
||
| 540 | else |
||
| 541 | { |
||
| 542 | $key = $this->primary_key; |
||
| 543 | $value = $args[0]; |
||
| 544 | } |
||
| 545 | $this->trigger('before_dropdown', array( $key, $value )); |
||
| 546 | if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
||
| 547 | { |
||
| 548 | $this->_database->where($this->soft_delete_key, FALSE); |
||
| 549 | } |
||
| 550 | $result = $this->_database->select(array($key, $value)) |
||
| 551 | ->from($this->_table) |
||
| 552 | ->getAll(); |
||
| 553 | $options = array(); |
||
| 554 | foreach ($result as $row) |
||
| 555 | { |
||
| 556 | $options[$row->{$key}] = $row->{$value}; |
||
| 557 | } |
||
| 558 | $options = $this->trigger('after_dropdown', $options); |
||
| 559 | return $options; |
||
| 560 | } |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Fetch a count of rows based on an arbitrary WHERE call. |
||
| 564 | */ |
||
| 565 | public function count_by() |
||
| 566 | { |
||
| 567 | if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
||
| 568 | { |
||
| 569 | $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
||
| 570 | } |
||
| 571 | $where = func_get_args(); |
||
| 572 | $this->_set_where($where); |
||
| 573 | $this->_database->from($this->_table)->getAll(); |
||
| 574 | return $this->_database->numRows(); |
||
| 575 | } |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Fetch a total count of rows, disregarding any previous conditions |
||
| 579 | */ |
||
| 580 | public function count_all() |
||
| 581 | { |
||
| 582 | if ($this->soft_delete && $this->_temporary_with_deleted !== TRUE) |
||
| 583 | { |
||
| 584 | $this->_database->where($this->soft_delete_key, (bool)$this->_temporary_only_deleted); |
||
| 585 | } |
||
| 586 | $this->_database->from($this->_table)->getAll(); |
||
| 587 | return $this->_database->numRows(); |
||
| 588 | } |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Enabled cache temporary |
||
| 592 | */ |
||
| 593 | public function cached($ttl = 0){ |
||
| 594 | if($ttl > 0){ |
||
| 595 | $this->_database = $this->_database->cached($ttl); |
||
| 596 | } |
||
| 597 | return $this; |
||
| 598 | } |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Tell the class to skip the insert validation |
||
| 602 | */ |
||
| 603 | public function skip_validation() |
||
| 604 | { |
||
| 605 | $this->skip_validation = TRUE; |
||
| 606 | return $this; |
||
| 607 | } |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Get the skip validation status |
||
| 611 | */ |
||
| 612 | public function get_skip_validation() |
||
| 613 | { |
||
| 614 | return $this->skip_validation; |
||
| 615 | } |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Return the next auto increment of the table. Only tested on MySQL. |
||
| 619 | */ |
||
| 620 | public function get_next_id() |
||
| 621 | { |
||
| 622 | return (int) $this->_database->select('AUTO_INCREMENT') |
||
| 623 | ->from('information_schema.TABLES') |
||
| 624 | ->where('TABLE_NAME', $this->_table) |
||
| 625 | ->where('TABLE_SCHEMA', $this->_database->getDatabaseName())->get()->AUTO_INCREMENT; |
||
| 626 | } |
||
| 627 | |||
| 628 | /** |
||
| 629 | * Getter for the table name |
||
| 630 | */ |
||
| 631 | public function table() |
||
| 632 | { |
||
| 633 | return $this->_table; |
||
| 634 | } |
||
| 635 | |||
| 636 | /* -------------------------------------------------------------- |
||
| 637 | * GLOBAL SCOPES |
||
| 638 | * ------------------------------------------------------------ */ |
||
| 639 | |||
| 640 | /** |
||
| 641 | * Return the next call as an array rather than an object |
||
| 642 | */ |
||
| 643 | public function as_array() |
||
| 644 | { |
||
| 645 | $this->_temporary_return_type = 'array'; |
||
| 646 | return $this; |
||
| 647 | } |
||
| 648 | |||
| 649 | /** |
||
| 650 | * Return the next call as an object rather than an array |
||
| 651 | */ |
||
| 652 | public function as_object() |
||
| 653 | { |
||
| 654 | $this->_temporary_return_type = 'object'; |
||
| 655 | return $this; |
||
| 656 | } |
||
| 657 | |||
| 658 | /** |
||
| 659 | * Don't care about soft deleted rows on the next call |
||
| 660 | */ |
||
| 661 | public function with_deleted() |
||
| 662 | { |
||
| 663 | $this->_temporary_with_deleted = TRUE; |
||
| 664 | return $this; |
||
| 665 | } |
||
| 666 | |||
| 667 | /** |
||
| 668 | * Only get deleted rows on the next call |
||
| 669 | */ |
||
| 670 | public function only_deleted() |
||
| 671 | { |
||
| 672 | $this->_temporary_only_deleted = TRUE; |
||
| 673 | return $this; |
||
| 674 | } |
||
| 675 | |||
| 676 | /* -------------------------------------------------------------- |
||
| 677 | * OBSERVERS |
||
| 678 | * ------------------------------------------------------------ */ |
||
| 679 | |||
| 680 | /** |
||
| 681 | * MySQL DATETIME created_at and updated_at |
||
| 682 | */ |
||
| 683 | public function created_at($row) |
||
| 684 | { |
||
| 685 | if (is_object($row)) |
||
| 686 | { |
||
| 687 | $row->created_at = date('Y-m-d H:i:s'); |
||
| 688 | } |
||
| 689 | else |
||
| 690 | { |
||
| 691 | $row['created_at'] = date('Y-m-d H:i:s'); |
||
| 692 | } |
||
| 693 | |||
| 694 | return $row; |
||
| 695 | } |
||
| 696 | |||
| 697 | public function updated_at($row) |
||
| 698 | { |
||
| 699 | if (is_object($row)) |
||
| 700 | { |
||
| 701 | $row->updated_at = date('Y-m-d H:i:s'); |
||
| 702 | } |
||
| 703 | else |
||
| 704 | { |
||
| 705 | $row['updated_at'] = date('Y-m-d H:i:s'); |
||
| 706 | } |
||
| 707 | return $row; |
||
| 708 | } |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Serialises data for you automatically, allowing you to pass |
||
| 712 | * through objects and let it handle the serialisation in the background |
||
| 713 | */ |
||
| 714 | public function serialize($row) |
||
| 715 | { |
||
| 716 | foreach ($this->callback_parameters as $column) |
||
| 717 | { |
||
| 718 | $row[$column] = serialize($row[$column]); |
||
| 719 | } |
||
| 720 | return $row; |
||
| 721 | } |
||
| 722 | |||
| 723 | public function unserialize($row) |
||
| 724 | { |
||
| 725 | foreach ($this->callback_parameters as $column) |
||
| 726 | { |
||
| 727 | if (is_array($row)) |
||
| 728 | { |
||
| 729 | $row[$column] = unserialize($row[$column]); |
||
| 730 | } |
||
| 731 | else |
||
| 732 | { |
||
| 733 | $row->$column = unserialize($row->$column); |
||
| 734 | } |
||
| 735 | } |
||
| 736 | return $row; |
||
| 737 | } |
||
| 738 | |||
| 739 | /** |
||
| 740 | * Protect attributes by removing them from $row array |
||
| 741 | */ |
||
| 742 | public function protect_attributes($row) |
||
| 743 | { |
||
| 744 | foreach ($this->protected_attributes as $attr) |
||
| 745 | { |
||
| 746 | if (is_object($row)) |
||
| 747 | { |
||
| 748 | if(isset($row->$attr)){ |
||
| 749 | unset($row->$attr); |
||
| 750 | } |
||
| 751 | } |
||
| 752 | else |
||
| 753 | { |
||
| 754 | if(isset($row[$attr])){ |
||
| 755 | unset($row[$attr]); |
||
| 756 | } |
||
| 757 | } |
||
| 758 | } |
||
| 759 | return $row; |
||
| 760 | } |
||
| 761 | |||
| 762 | /** |
||
| 763 | * Return the database instance |
||
| 764 | * @return Database the database instance |
||
| 765 | */ |
||
| 766 | public function getDatabaseInstance(){ |
||
| 767 | return $this->_database; |
||
| 768 | } |
||
| 769 | |||
| 770 | /** |
||
| 771 | * set the Database instance for future use |
||
| 772 | * @param Database $db the database object |
||
| 773 | */ |
||
| 774 | public function setDatabaseInstance($db){ |
||
| 780 | } |
||
| 781 | |||
| 782 | /** |
||
| 783 | * Return the loader instance |
||
| 784 | * @return Loader the loader instance |
||
| 785 | */ |
||
| 786 | public function getLoader(){ |
||
| 787 | return $this->loaderInstance; |
||
| 788 | } |
||
| 789 | |||
| 790 | /** |
||
| 791 | * set the loader instance for future use |
||
| 792 | * @param Loader $loader the loader object |
||
| 793 | */ |
||
| 794 | public function setLoader($loader){ |
||
| 795 | $this->loaderInstance = $loader; |
||
| 796 | return $this; |
||
| 797 | } |
||
| 798 | |||
| 799 | /** |
||
| 800 | * Return the FormValidation instance |
||
| 801 | * @return FormValidation the form validation instance |
||
| 802 | */ |
||
| 803 | public function getFormValidation(){ |
||
| 804 | return $this->formValidationInstance; |
||
| 805 | } |
||
| 806 | |||
| 807 | /** |
||
| 808 | * set the form validation instance for future use |
||
| 809 | * @param FormValidation $fv the form validation object |
||
| 810 | */ |
||
| 811 | public function setFormValidation($fv){ |
||
| 812 | $this->formValidationInstance = $fv; |
||
| 813 | return $this; |
||
| 814 | } |
||
| 815 | |||
| 816 | /* -------------------------------------------------------------- |
||
| 817 | * QUERY BUILDER DIRECT ACCESS METHODS |
||
| 818 | * ------------------------------------------------------------ */ |
||
| 819 | |||
| 820 | /** |
||
| 821 | * A wrapper to $this->_database->orderBy() |
||
| 822 | */ |
||
| 823 | public function order_by($criteria, $order = 'ASC') |
||
| 824 | { |
||
| 825 | if ( is_array($criteria) ) |
||
| 826 | { |
||
| 827 | foreach ($criteria as $key => $value) |
||
| 828 | { |
||
| 829 | $this->_database->orderBy($key, $value); |
||
| 830 | } |
||
| 831 | } |
||
| 832 | else |
||
| 833 | { |
||
| 834 | $this->_database->orderBy($criteria, $order); |
||
| 835 | } |
||
| 836 | return $this; |
||
| 837 | } |
||
| 838 | |||
| 839 | /** |
||
| 840 | * A wrapper to $this->_database->limit() |
||
| 841 | */ |
||
| 842 | public function limit($offset = 0, $limit = 10) |
||
| 843 | { |
||
| 844 | $this->_database->limit($offset, $limit); |
||
| 845 | return $this; |
||
| 846 | } |
||
| 847 | |||
| 848 | /* -------------------------------------------------------------- |
||
| 849 | * INTERNAL METHODS |
||
| 850 | * ------------------------------------------------------------ */ |
||
| 851 | |||
| 852 | /** |
||
| 853 | * Trigger an event and call its observers. Pass through the event name |
||
| 854 | * (which looks for an instance variable $this->event_name), an array of |
||
| 855 | * parameters to pass through and an optional 'last in interation' boolean |
||
| 856 | */ |
||
| 857 | protected function trigger($event, $data = FALSE, $last = TRUE) |
||
| 874 | } |
||
| 875 | |||
| 876 | /** |
||
| 877 | * Run validation on the passed data |
||
| 878 | */ |
||
| 879 | protected function validate(array $data) |
||
| 880 | { |
||
| 881 | if($this->skip_validation) |
||
| 882 | { |
||
| 883 | return $data; |
||
| 884 | } |
||
| 885 | if(!empty($this->validate)) |
||
| 886 | { |
||
| 887 | $fv = null; |
||
| 888 | if(is_object($this->formValidationInstance)){ |
||
| 889 | $fv = $this->formValidationInstance; |
||
| 890 | } |
||
| 891 | else{ |
||
| 892 | Loader::library('FormValidation'); |
||
| 893 | $fv = $this->formvalidation; |
||
| 894 | $this->setFormValidation($fv); |
||
| 895 | } |
||
| 896 | |||
| 897 | $fv->setData($data); |
||
| 898 | $fv->setRules($this->validate); |
||
| 899 | |||
| 900 | if ($fv->run()) |
||
| 901 | { |
||
| 902 | return $data; |
||
| 903 | } |
||
| 904 | else |
||
| 905 | { |
||
| 906 | return FALSE; |
||
| 907 | } |
||
| 908 | } |
||
| 909 | else |
||
| 910 | { |
||
| 911 | return $data; |
||
| 912 | } |
||
| 913 | } |
||
| 914 | |||
| 915 | |||
| 916 | /** |
||
| 917 | * Set WHERE parameters, cleverly |
||
| 918 | */ |
||
| 919 | protected function _set_where($params) |
||
| 970 | } |
||
| 971 | } |
||
| 972 | } |
||
| 973 | |||
| 974 | /** |
||
| 975 | Shortcut to controller |
||
| 976 | */ |
||
| 977 | public function __get($key){ |
||
| 979 | } |
||
| 980 | |||
| 982 |