From 1b4c445cadc7dac76e93b67acba4a6c22def44dd Mon Sep 17 00:00:00 2001 From: phpnut Date: Sun, 16 Mar 2008 07:18:42 +0000 Subject: [PATCH] "Fixes #4256, tablePrefix + HABTM with defaults == wrong joinTable" git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6585 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/class_registry.php | 7 ++++++- cake/libs/model/datasources/dbo_source.php | 13 +++++++------ cake/tests/cases/dispatcher.test.php | 2 +- cake/tests/cases/libs/class_registry.test.php | 19 +++++++++++++++++++ 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/cake/libs/class_registry.php b/cake/libs/class_registry.php index a3e354965..9d44239af 100644 --- a/cake/libs/class_registry.php +++ b/cake/libs/class_registry.php @@ -127,7 +127,12 @@ class ClassRegistry { if (App::import($type, $plugin . $class)) { ${$class} =& new $class($options); } elseif ($type === 'Model') { - ${$class} =& new AppModel($options); + if ($plugin && class_exists($plugin .'AppModel')) { + $appModel = $plugin .'AppModel'; + } else { + $appModel = 'AppModel'; + } + ${$class} =& new $appModel($options); } if (!isset(${$class})) { diff --git a/cake/libs/model/datasources/dbo_source.php b/cake/libs/model/datasources/dbo_source.php index 301f63c24..07c36f6fe 100644 --- a/cake/libs/model/datasources/dbo_source.php +++ b/cake/libs/model/datasources/dbo_source.php @@ -1035,23 +1035,26 @@ class DboSource extends DataSource { ); break; case 'hasAndBelongsToMany': - $joinTbl = $this->fullTableName($assocData['joinTable']); $joinFields = array(); $joinAssoc = null; - $joinAlias = $joinTbl; if (isset($assocData['with']) && !empty($assocData['with'])) { $joinKeys = array($assocData['foreignKey'], $assocData['associationForeignKey']); list($with, $joinFields) = $model->joinModel($assocData['with'], $joinKeys); + $joinTbl = $this->fullTableName($model->{$with}); + $joinAlias = $joinTbl; + if (is_array($joinFields) && !empty($joinFields)) { $joinFields = $this->fields($model->{$with}, $model->{$with}->alias, $joinFields); $joinAssoc = $joinAlias = $model->{$with}->alias; } else { $joinFields = array(); } + } else { + $joinTbl = $this->fullTableName($assocData['joinTable']); + $joinAlias = $joinTbl; } - $query = array( 'conditions' => $assocData['conditions'], 'limit' => $assocData['limit'], @@ -1062,9 +1065,7 @@ class DboSource extends DataSource { 'joins' => array(array( 'table' => $joinTbl, 'alias' => $joinAssoc, - 'conditions' => $this->getConstraint('hasAndBelongsToMany', $model, $linkModel, $joinAlias, $assocData, $alias) - )) - ); + 'conditions' => $this->getConstraint('hasAndBelongsToMany', $model, $linkModel, $joinAlias, $assocData, $alias)))); break; } if (isset($query)) { diff --git a/cake/tests/cases/dispatcher.test.php b/cake/tests/cases/dispatcher.test.php index b599b02cb..1bbb572d2 100644 --- a/cake/tests/cases/dispatcher.test.php +++ b/cake/tests/cases/dispatcher.test.php @@ -992,7 +992,7 @@ class DispatcherTest extends UnitTestCase { $this->assertEqual($result, $expected); $filename = CACHE . 'views' . DS . Inflector::slug($dispatcher->here) . '.php'; unlink($filename); - + $url = 'TestCachedPages/index'; restore_error_handler(); diff --git a/cake/tests/cases/libs/class_registry.test.php b/cake/tests/cases/libs/class_registry.test.php index b1c21dd22..d5820edcb 100644 --- a/cake/tests/cases/libs/class_registry.test.php +++ b/cake/tests/cases/libs/class_registry.test.php @@ -42,7 +42,16 @@ class RegisterArticleTag extends ClassRegisterModel { var $name = 'RegisterArticlTag'; } +class RegistryPluginAppModel extends ClassRegisterModel { + var $tablePrefix = 'something_'; +} + +class TestRegistryPluginModel extends RegistryPluginAppModel { + var $name = 'TestRegistryPluginModel'; +} + class ClassRegistryTest extends UnitTestCase { + function testAddModel() { if (PHP5) { $Tag = ClassRegistry::init('RegisterArticleTag'); @@ -124,5 +133,15 @@ class ClassRegistryTest extends UnitTestCase { $Tag = ClassRegistry::getObject('Tag'); $this->assertTrue(is_a($Tag, 'RegisterArticleTag')); } + + function testPluginAppModel() { + $TestRegistryPluginModel = ClassRegistry::isKeySet('TestRegistryPluginModel'); + $this->assertFalse($TestRegistryPluginModel); + + $TestRegistryPluginModel = ClassRegistry::init('RegistryPlugin.TestRegistryPluginModel'); + $this->assertTrue(is_a($TestRegistryPluginModel, 'TestRegistryPluginModel')); + + $this->assertEqual($TestRegistryPluginModel->tablePrefix, 'something_'); + } } ?> \ No newline at end of file