From c75b09fd0d96a3871a8b02cbf0182d09b80d96e4 Mon Sep 17 00:00:00 2001 From: admin Date: Tue, 24 Oct 2006 00:14:28 +0000 Subject: --- system/libraries/Model.php | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/system/libraries/Model.php b/system/libraries/Model.php index 240db4397..dfb48a5d0 100644 --- a/system/libraries/Model.php +++ b/system/libraries/Model.php @@ -26,6 +26,8 @@ */ class Model { + var $_parent_name = ''; + /** * Constructor * @@ -33,7 +35,17 @@ class Model { */ function Model() { - $this->_assign_libraries(); + // If the magic __get() method is used in a Model references can't be used. + $this->_assign_libraries( (method_exists($this, '__get')) ? FALSE : TRUE ); + + // We don't want to assign the model object to itself when using the + // assign_libraries function below so we'll grab the name of the model parent + $methods = get_class_methods($this); + if (isset($methods[0])) + { + $this->_parent_name = $methods[0]; + } + log_message('debug', "Model Class Initialized"); } @@ -42,17 +54,30 @@ class Model { * * Creates local references to all currently instantiated objects * so that any syntax that can be legally used in a controller - * can be used within models. + * can be used within models. * * @access private */ - function _assign_libraries() + function _assign_libraries($use_reference = TRUE) { - $CI =& get_instance(); - + $CI =& get_instance(); foreach (array_keys(get_object_vars($CI)) as $key) { - $this->$key =& $CI->$key; + if ( ! isset($this->$key) AND $key != $this->_parent_name) + { + // In some cases using references can cause + // problems so we'll conditionally use them + if ($use_reference == TRUE) + { + // Needed to prevent reference errors with some configurations + $this->$key = ''; + $this->$key =& $CI->$key; + } + else + { + $this->$key = $CI->$key; + } + } } } -- cgit v1.2.3-24-g4f1b