Created
March 26, 2012 22:51
-
-
Save kemo/2210398 to your computer and use it in GitHub Desktop.
Speed up Kohana ORM init
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php defined('SYSPATH') or die('No direct script access.'); | |
class Kohana_ORM extends Model implements serializable { | |
/** | |
* Initialization storage for ORM models | |
* @var array | |
*/ | |
protected static $_init_cache = array(); | |
/** | |
* Prepares the model database connection, determines the table name, | |
* and loads column information. | |
* | |
* @return void | |
*/ | |
protected function _initialize() | |
{ | |
// Set the object name and plural name | |
$this->_object_name = strtolower(substr(get_class($this), 6)); | |
// Check if this model has already been initialized | |
if ( ! $init = Arr::get(ORM::$_init_cache, $this->_object_name, FALSE)) | |
{ | |
$init = array( | |
'_belongs_to' => array(), | |
'_has_one' => array(), | |
'_has_many' => array(), | |
); | |
// Set the object plural name if none predefined | |
if ( ! isset($this->_object_plural)) | |
{ | |
$init['_object_plural'] = Inflector::plural($this->_object_name); | |
} | |
if ( ! $this->_errors_filename) | |
{ | |
$init['_errors_filename'] = $this->_object_name; | |
} | |
if ( ! is_object($this->_db)) | |
{ | |
// Get database instance | |
$init['_db'] = Database::instance($this->_db_group); | |
} | |
if (empty($this->_table_name)) | |
{ | |
// Table name is the same as the object name | |
$init['_table_name'] = $this->_object_name; | |
if ($this->_table_names_plural === TRUE) | |
{ | |
// Make the table name plural | |
$init['_table_name'] = Arr::get($init, '_object_plural', $this->_object_plural); | |
} | |
} | |
$defaults = array(); | |
foreach ($this->_belongs_to as $alias => $details) | |
{ | |
$defaults['model'] = $alias; | |
$defaults['foreign_key'] = $alias.$this->_foreign_key_suffix; | |
$init['_belongs_to'][$alias] = array_merge($defaults, $details); | |
} | |
foreach ($this->_has_one as $alias => $details) | |
{ | |
$defaults['model'] = $alias; | |
$defaults['foreign_key'] = $this->_object_name.$this->_foreign_key_suffix; | |
$init['_has_one'][$alias] = array_merge($defaults, $details); | |
} | |
foreach ($this->_has_many as $alias => $details) | |
{ | |
if ( ! isset($details['model'])) | |
{ | |
$defaults['model'] = Inflector::singular($alias); | |
} | |
$defaults['foreign_key'] = $this->_object_name.$this->_foreign_key_suffix; | |
$defaults['through'] = NULL; | |
if ( ! isset($details['far_key'])) | |
{ | |
$defaults['far_key'] = Inflector::singular($alias).$this->_foreign_key_suffix; | |
} | |
$init['_has_many'][$alias] = array_merge($defaults, $details); | |
} | |
ORM::$_init_cache[$this->_object_name] = $init; | |
} | |
foreach ($init as $property => $value) | |
{ | |
$this->$property = $value; | |
} | |
// Load column information | |
$this->reload_columns(); | |
// Clear initial model state | |
$this->clear(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment