From e165f7d5594292dc6588776ec2bd8c2319a59845 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Lorenzo=20Rodr=C3=ADguez=20Urdaneta?= Date: Wed, 14 Jul 2010 23:19:38 -0430 Subject: [PATCH] Changing how dynamic "with" models are loaded Changing all calls to get model's datasourse to use Model::getDatasource() --- cake/libs/model/model.php | 75 +++++++++---------- .../libs/model/model_integration.test.php | 14 ++-- 2 files changed, 44 insertions(+), 45 deletions(-) diff --git a/cake/libs/model/model.php b/cake/libs/model/model.php index 318dd7d50..b2fbf4b0a 100644 --- a/cake/libs/model/model.php +++ b/cake/libs/model/model.php @@ -492,8 +492,7 @@ class Model extends Object { if ($result !== array('unhandled')) { return $result; } - $db =& ConnectionManager::getDataSource($this->useDbConfig); - $return = $db->query($method, $params, $this); + $return = $this->getDataSource()->query($method, $params, $this); return $return; } @@ -515,8 +514,10 @@ class Model extends Object { if (empty($relation['with'])) { continue; } - if (is_array($relation['with']) && key($relation['with']) === $name){ - $className = $name; + if (is_array($relation['with'])) { + if (key($relation['with']) === $name) { + $className = $name; + } } else { list($plugin, $class) = pluginSplit($relation['with']); if ($class === $name) { @@ -525,6 +526,7 @@ class Model extends Object { } if ($className) { $assocKey = $k; + $dynamic = !empty($relation['dynamicWith']); break(2); } } @@ -536,7 +538,17 @@ class Model extends Object { } list($plugin, $className) = pluginSplit($className); - $this->__constructLinkedModel($name, $className, $plugin); + + if (!ClassRegistry::isKeySet($className) && !empty($dynamic)) { + $this->{$className} = new AppModel(array( + 'name' => $className, + 'table' => $this->hasAndBelongsToMany[$assocKey]['joinTable'], + 'ds' => $this->useDbConfig + )); + } else { + $this->__constructLinkedModel($name, $className, $plugin); + } + if (!empty($assocKey)) { $this->hasAndBelongsToMany[$assocKey]['joinTable'] = $this->{$name}->table; if (count($this->{$name}->schema()) <= 2 && $this->{$name}->primaryKey !== false) { @@ -757,22 +769,11 @@ class Model extends Object { } $this->{$type}[$assocKey][$key] = $data; } - } - if (!empty($this->{$type}[$assocKey]['with'])) { - $joinClass = $this->{$type}[$assocKey]['with']; - if (is_array($joinClass)) { - $joinClass = key($joinClass); + if ($dynamicWith) { + $this->{$type}[$assocKey]['dynamicWith'] = true; } - list($plugin, $joinClass) = pluginSplit($joinClass); - if (!ClassRegistry::isKeySet($joinClass) && $dynamicWith === true) { - $this->{$joinClass} = new AppModel(array( - 'name' => $joinClass, - 'table' => $this->{$type}[$assocKey]['joinTable'], - 'ds' => $this->useDbConfig - )); - } } } @@ -784,7 +785,7 @@ class Model extends Object { */ public function setSource($tableName) { $this->setDataSource($this->useDbConfig); - $db =& ConnectionManager::getDataSource($this->useDbConfig); + $db = $this->getDataSource(); $db->cacheSources = ($this->cacheSources && $db->cacheSources); if ($db->isInterfaceSupported('listSources')) { @@ -882,7 +883,7 @@ class Model extends Object { $dateFields = array('Y' => 'year', 'm' => 'month', 'd' => 'day', 'H' => 'hour', 'i' => 'min', 's' => 'sec'); $timeFields = array('H' => 'hour', 'i' => 'min', 's' => 'sec'); - $db =& ConnectionManager::getDataSource($this->useDbConfig); + $db = $this->getDataSource(); $format = $db->columns[$type]['format']; $date = array(); @@ -943,7 +944,7 @@ class Model extends Object { */ public function schema($field = false) { if (!is_array($this->_schema) || $field === true) { - $db =& ConnectionManager::getDataSource($this->useDbConfig); + $db = $this->getDataSource(); $db->cacheSources = ($this->cacheSources && $db->cacheSources); if ($db->isInterfaceSupported('describe') && $this->useTable !== false) { $this->_schema = $db->describe($this, $field); @@ -985,7 +986,7 @@ class Model extends Object { * @return string Column type */ public function getColumnType($column) { - $db =& ConnectionManager::getDataSource($this->useDbConfig); + $db = $this->getDataSource(); $cols = $this->schema(); $model = null; @@ -1272,7 +1273,7 @@ class Model extends Object { return false; } - $db =& ConnectionManager::getDataSource($this->useDbConfig); + $db = $this->getDataSource(); foreach ($dateFields as $updateCol) { if ($this->hasField($updateCol) && !in_array($updateCol, $fields)) { @@ -1589,7 +1590,7 @@ class Model extends Object { if (empty($data)) { $data = $this->data; } - $db =& ConnectionManager::getDataSource($this->useDbConfig); + $db = $this->getDataSource(); $options = array_merge(array('validate' => 'first', 'atomic' => true), $options); $this->validationErrors = $validationErrors = array(); @@ -1794,8 +1795,7 @@ class Model extends Object { * @link http://book.cakephp.org/view/1031/Saving-Your-Data */ function updateAll($fields, $conditions = true) { - $db =& ConnectionManager::getDataSource($this->useDbConfig); - return $db->update($this, $fields, null, $conditions); + return $this->getDataSource()->update($this, $fields, null, $conditions); } /** @@ -1814,7 +1814,7 @@ class Model extends Object { $id = $this->id; if ($this->exists() && $this->beforeDelete($cascade)) { - $db =& ConnectionManager::getDataSource($this->useDbConfig); + $db = $this->getDataSource(); $filters = $this->Behaviors->trigger($this, 'beforeDelete', array($cascade), array( 'break' => true, 'breakOn' => false )); @@ -1924,7 +1924,7 @@ class Model extends Object { if (empty($conditions)) { return false; } - $db =& ConnectionManager::getDataSource($this->useDbConfig); + $db = $this->getDataSource(); if (!$cascade && !$callbacks) { return $db->delete($this, $conditions); @@ -2097,7 +2097,7 @@ class Model extends Object { } } - if (!$db =& ConnectionManager::getDataSource($this->useDbConfig)) { + if (!$db = $this->getDataSource()) { return false; } @@ -2153,7 +2153,7 @@ class Model extends Object { */ function _findCount($state, $query, $results = array()) { if ($state == 'before') { - $db =& ConnectionManager::getDataSource($this->useDbConfig); + $db = $this->getDataSource(); if (empty($query['fields'])) { $query['fields'] = $db->calculate($this, 'count'); } elseif (is_string($query['fields']) && !preg_match('/count/i', $query['fields'])) { @@ -2432,7 +2432,7 @@ class Model extends Object { */ function query() { $params = func_get_args(); - $db =& ConnectionManager::getDataSource($this->useDbConfig); + $db = $this->getDataSource(); return call_user_func_array(array(&$db, 'query'), $params); } @@ -2694,7 +2694,7 @@ class Model extends Object { if (empty($field)) { $field = $this->primaryKey; } - $db =& ConnectionManager::getDataSource($this->useDbConfig); + $db = $this->getDataSource(); if (strpos($field, $db->name($alias) . '.') === 0) { return $field; } @@ -2766,8 +2766,7 @@ class Model extends Object { * @return int Number of rows */ public function getNumRows() { - $db =& ConnectionManager::getDataSource($this->useDbConfig); - return $db->lastNumRows(); + return $this->getDataSource()->lastNumRows(); } /** @@ -2776,8 +2775,7 @@ class Model extends Object { * @return int Number of rows */ public function getAffectedRows() { - $db =& ConnectionManager::getDataSource($this->useDbConfig); - return $db->lastAffected(); + return $this->getDataSource()->lastAffected(); } /** @@ -2792,7 +2790,7 @@ class Model extends Object { if ($dataSource != null) { $this->useDbConfig = $dataSource; } - $db =& ConnectionManager::getDataSource($this->useDbConfig); + $db = $this->getDataSource(); if (!empty($oldConfig) && isset($db->config['prefix'])) { $oldDb =& ConnectionManager::getDataSource($oldConfig); @@ -2815,8 +2813,7 @@ class Model extends Object { * @return object A DataSource object */ public function &getDataSource() { - $db =& ConnectionManager::getDataSource($this->useDbConfig); - return $db; + return ConnectionManager::getDataSource($this->useDbConfig); } /** diff --git a/cake/tests/cases/libs/model/model_integration.test.php b/cake/tests/cases/libs/model/model_integration.test.php index c74109bac..384a5370f 100644 --- a/cake/tests/cases/libs/model/model_integration.test.php +++ b/cake/tests/cases/libs/model/model_integration.test.php @@ -57,7 +57,7 @@ class ModelIntegrationTest extends BaseModelTest { * @return void */ public function testAssociationLazyLoading() { - $this->loadFixtures('ArticleFeatured', 'User', 'Category', 'Comment', 'ArticleFeaturedsTags'); + $this->loadFixtures('ArticleFeatured', 'User', 'Category', 'Comment', 'ArticleFeaturedsTags', 'Tag'); $Article = new ArticleFeatured(); $this->assertTrue(isset($Article->belongsTo['User'])); $this->assertFalse(property_exists($Article, 'User')); @@ -79,7 +79,8 @@ class ModelIntegrationTest extends BaseModelTest { $this->assertTrue(property_exists($Article, 'Tag')); $this->assertTrue(isset($Article->Tag)); $this->assertType('Tag', $Article->Tag); - $this->assertTrue(property_exists($Article, 'ArticleFeaturedsTag')); + + $this->assertFalse(property_exists($Article, 'ArticleFeaturedsTag')); $this->assertType('AppModel', $Article->ArticleFeaturedsTag); $this->assertEquals($Article->hasAndBelongsToMany['Tag']['joinTable'], 'article_featureds_tags'); $this->assertEquals($Article->hasAndBelongsToMany['Tag']['associationForeignKey'], 'tag_id'); @@ -92,14 +93,13 @@ class ModelIntegrationTest extends BaseModelTest { * @return void */ public function testAssociationLazyLoadWithHABTM() { - $this->loadFixtures('Article', 'UuidTag', 'Fruit', 'FruitsUuidTag'); + $this->loadFixtures('Article', 'UuidTag', 'Fruit', 'FruitsUuidTag', 'ArticlesTag'); $Article = new ArticleB(); $this->assertTrue(isset($Article->hasAndBelongsToMany['TagB'])); $this->assertFalse(property_exists($Article, 'TagB')); $this->assertType('TagB', $Article->TagB); - //Dynamic "with" models are not lazy loaded - $this->assertTrue(property_exists($Article, 'ArticlesTag')); + $this->assertFalse(property_exists($Article, 'ArticlesTag')); $this->assertType('AppModel', $Article->ArticlesTag); $UuidTag = new UuidTag(); @@ -107,7 +107,7 @@ class ModelIntegrationTest extends BaseModelTest { $this->assertFalse(property_exists($UuidTag, 'Fruit')); $this->assertFalse(property_exists($UuidTag, 'FruitsUuidTag')); $this->assertTrue(isset($UuidTag->Fruit)); - //But non-dynamic with models are lazy loaded + $this->assertFalse(property_exists($UuidTag, 'FruitsUuidTag')); $this->assertTrue(isset($UuidTag->FruitsUuidTag)); $this->assertType('FruitsUuidTag', $UuidTag->FruitsUuidTag); @@ -1199,6 +1199,7 @@ class ModelIntegrationTest extends BaseModelTest { 'foreignKey' => false, 'className' => 'AssociationTest2', 'with' => 'JoinAsJoinB', + 'dynamicWith' => true, 'associationForeignKey' => 'join_b_id', 'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '', 'finderQuery' => '', 'deleteQuery' => '', 'insertQuery' => '' @@ -1269,6 +1270,7 @@ class ModelIntegrationTest extends BaseModelTest { 'className' => 'Tag', 'joinTable' => 'article_featureds_tags', 'with' => 'ArticleFeaturedsTag', + 'dynamicWith' => true, 'foreignKey' => 'article_featured_id', 'associationForeignKey' => 'tag_id', 'conditions' => '',