From 02d90348f869f1caab6f143802b3aee9deb814e5 Mon Sep 17 00:00:00 2001 From: phpnut Date: Mon, 20 Feb 2006 03:03:05 +0000 Subject: [PATCH] Merging fixes and enhancements into trunk. Revision: [2064] Added doc block for Model::__resetAssocitions(); Removed unused params in Model::__constructLinkedModel(); Revision: [2063] Refactored bindTo to allow method to be used to change associations settings. This method will change the association on the fly, and reset them back once the query has completed. This will allow something like this simple example. $this->Book->bindToAuthor(array('belongsTo' => array('conditions' => array('Author.active'=>'1')))); $this->set('changed', $this->Book->findAll()); $this->set('original', $this->Book->findAll()); $changed would have the associated for Books/Authors where Author.active = 1. $original would have the associations for Books that where set in the Book class association vars. Revision: [2062] Added magic method to allow late binding of association model. Revision: [2058] Adding fix for error in acl.php reported in google group git-svn-id: https://svn.cakephp.org/repo/trunk/cake@2065 3807eeeb-6ff5-0310-8944-8be069107fe0 --- VERSION.txt | 2 +- cake/libs/model/model_php4.php | 56 +++++++++++++++++++++++++++++----- cake/libs/model/model_php5.php | 54 ++++++++++++++++++++++++++++---- cake/scripts/acl.php | 24 ++++++--------- 4 files changed, 107 insertions(+), 29 deletions(-) diff --git a/VERSION.txt b/VERSION.txt index ecb2d1913..827e9fdf6 100644 --- a/VERSION.txt +++ b/VERSION.txt @@ -6,4 +6,4 @@ // +---------------------------------------------------------------------------------------------------+ // /////////////////////////////////////////////////////////////////////////////////////////////////////////// -0.10.8.2057 \ No newline at end of file +0.10.8.2065 \ No newline at end of file diff --git a/cake/libs/model/model_php4.php b/cake/libs/model/model_php4.php index 729aa2b65..0364070b6 100644 --- a/cake/libs/model/model_php4.php +++ b/cake/libs/model/model_php4.php @@ -368,7 +368,8 @@ class Model extends Object * PHP4 Only * * Handles custom method calls, like findBy for DB models, - * and custom RPC calls for remote data sources. + * custom RPC calls for remote data sources and bind to model calls + * to change associations on the fly. * * @param unknown_type $method * @param unknown_type $params @@ -378,8 +379,22 @@ class Model extends Object */ function __call($method, $params, &$return) { - $return = $this->db->query($method, $params, $this); - return true; + $args = func_get_args(); + if (count($args) > 1 && strpos(low($args[0]), 'bindto') === 0) + { + $assoc = Inflector::camelize(preg_replace('/bindto/i', '', $args[0])); + $this->__constructLinkedModel($assoc, $assoc); + $type = array_keys($args[1][0]); + $this->__backAssociation[$type[0]] = $this->{$type[0]}; + $this->{$type[0]}[$assoc] = $args[1][0][$type[0]]; + $this->__generateAssociation($type[0], $assoc); + return true; + } + else + { + $return = $this->db->query($method, $params, $this); + return true; + } } /** @@ -410,7 +425,7 @@ class Model extends Object { $className = $value['className']; } - $this->__constructLinkedModel($assoc, $className, $type); + $this->__constructLinkedModel($assoc, $className); } } @@ -428,10 +443,9 @@ class Model extends Object * @param string $assoc * @param string $className Class name * @param string $type Type of assocation - * @todo Is the third parameter in use at the moment? It is not referred to in the method OJ, 30. jan 2006 * @access private */ - function __constructLinkedModel($assoc, $className, $type) + function __constructLinkedModel($assoc, $className) { $colKey = Inflector::underscore($className); if(ClassRegistry::isKeySet($colKey)) @@ -1134,7 +1148,35 @@ class Model extends Object 'limit' => $limit_str, 'order' => $order ); - return $this->afterFind($this->db->read($this, $queryData, $recursive)); + $return = $this->afterFind($this->db->read($this, $queryData, $recursive)); + + if(isset($this->__backAssociation)) + { + $this->__resetAssocitions(); + } + return $return; + } + + +/** + * Method is called only when bindTo() is used. + * This resets the association arrays for the model back + * to the original as set in the model. + * + * @return unknown + * @access private + */ + function __resetAssocitions() + { + foreach ($this->__associations as $type) + { + if(isset($this->__backAssociation[$type])) + { + $this->{$type} = $this->__backAssociation[$type]; + } + } + unset($this->__backAssociation); + return true; } /** diff --git a/cake/libs/model/model_php5.php b/cake/libs/model/model_php5.php index d9d9c869c..ac5130faa 100644 --- a/cake/libs/model/model_php5.php +++ b/cake/libs/model/model_php5.php @@ -366,7 +366,8 @@ class Model extends Object /** * Handles custom method calls, like findBy for DB models, - * and custom RPC calls for remote data sources. + * custom RPC calls for remote data sources and bind to model calls + * to change associations on the fly. * * @param unknown_type $method * @param array $params @@ -375,7 +376,21 @@ class Model extends Object */ function __call($method, $params) { - return $this->db->query($method, $params, $this); + $args = func_get_args(); + if (count($args) > 1 && strpos(low($args[0]), 'bindto') === 0) + { + $assoc = preg_replace('/bindto/i', '', $args[0]); + $this->__constructLinkedModel($assoc, $assoc); + $type = array_keys($args[1][0]); + $this->__backAssociation[$type[0]] = $this->{$type[0]}; + $this->{$type[0]}[$assoc] = $args[1][0][$type[0]]; + $this->__generateAssociation($type[0], $assoc); + return true; + } + else + { + return $this->db->query($method, $params, $this); + } } /** @@ -406,7 +421,7 @@ class Model extends Object { $className = $value['className']; } - $this->__constructLinkedModel($assoc, $className, $type); + $this->__constructLinkedModel($assoc, $className); } } @@ -424,10 +439,9 @@ class Model extends Object * @param string $assoc * @param string $className Class name * @param string $type Type of assocation - * @todo Is the third parameter in use at the moment? It is not referred to in the method OJ, 30. jan 2006 * @access private */ - function __constructLinkedModel($assoc, $className, $type) + function __constructLinkedModel($assoc, $className) { $colKey = Inflector::underscore($className); if(ClassRegistry::isKeySet($colKey)) @@ -1129,7 +1143,35 @@ class Model extends Object 'limit' => $limit_str, 'order' => $order ); - return $this->afterFind($this->db->read($this, $queryData, $recursive)); + $return = $this->afterFind($this->db->read($this, $queryData, $recursive)); + + if(isset($this->__backAssociation)) + { + $this->__resetAssocitions(); + } + return $return; + } + + +/** + * Method is called only when bindTo() is used. + * This resets the association arrays for the model back + * to the original as set in the model. + * + * @return unknown + * @access private + */ + function __resetAssocitions() + { + foreach ($this->__associations as $type) + { + if(isset($this->__backAssociation[$type])) + { + $this->{$type} = $this->__backAssociation[$type]; + } + } + unset($this->__backAssociation); + return true; } /** diff --git a/cake/scripts/acl.php b/cake/scripts/acl.php index 60dcdf0c9..33a2b73f3 100644 --- a/cake/scripts/acl.php +++ b/cake/scripts/acl.php @@ -31,28 +31,22 @@ ini_set('display_errors', '1'); ini_set('error_reporting', '7'); -/** - * Enter description here... - * - */ define ('DS', DIRECTORY_SEPARATOR); + /** - * Enter description here... + * Change these setting only if your install + * is different from a distribution install. * */ define ('ROOT', dirname(dirname(dirname(__FILE__))).DS); -/** - * Enter description here... - * - */ define ('APP_DIR', 'app'); -/** - * Enter description here... - * - */ +define('CAKE_CORE_INCLUDE_PATH', ROOT); + define ('DEBUG', 1); -require (ROOT.'cake'.DS.'basics.php'); -require (ROOT.'cake'.DS.'config'.DS.'paths.php'); +ini_set('include_path',ini_get('include_path').PATH_SEPARATOR.CAKE_CORE_INCLUDE_PATH.PATH_SEPARATOR.ROOT.DS.APP_DIR.DS); + +require ('cake'.DS.'basics.php'); +require ('cake'.DS.'config'.DS.'paths.php'); require (CONFIGS.'core.php'); require (CONFIGS.'database.php'); uses ('neat_array');