From e35a2158830582e94d466ad24e6488457e69b273 Mon Sep 17 00:00:00 2001 From: gwoo Date: Mon, 25 Aug 2008 22:33:36 +0000 Subject: [PATCH] fixes #5307, model name being overridden by class registry for no reason, thanks to vizjerai for the helpful ticket. git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7501 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/class_registry.php | 20 +++++------ cake/libs/model/model.php | 5 ++- cake/tests/cases/libs/class_registry.test.php | 34 +++++++++---------- .../cases/libs/controller/controller.test.php | 4 ++- 4 files changed, 32 insertions(+), 31 deletions(-) diff --git a/cake/libs/class_registry.php b/cake/libs/class_registry.php index ceadaf3d4..98f5ee658 100644 --- a/cake/libs/class_registry.php +++ b/cake/libs/class_registry.php @@ -106,49 +106,45 @@ class ClassRegistry { $objects = array(array('class' => $class)); } - $defaults = array_merge( - array('id' => false, 'table' => null, 'ds' => null, 'alias' => null, 'name' => null), - isset($_this->__config[$type]) ? $_this->__config[$type] : array() - ); + $defaults = isset($_this->__config[$type]) ? $_this->__config[$type] : array(); $count = count($objects); foreach ($objects as $key => $settings) { if (is_array($settings)) { $plugin = null; $settings = array_merge($defaults, $settings); - extract($settings, EXTR_OVERWRITE); + $class = $settings['class']; if (strpos($class, '.') !== false) { list($plugin, $class) = explode('.', $class); $plugin = $plugin . '.'; } - if (empty($alias)) { - $alias = $class; + if (empty($settings['alias'])) { + $settings['alias'] = $class; } + $alias = $settings['alias']; if ($model =& $_this->_duplicate($alias, $class)) { $_this->map($alias, $class); return $model; } - if ($type === 'Model') { - $options = array('id' => $id, 'table' => $table, 'ds' => $ds, 'alias' => $alias, 'name' => $class); - } if (App::import($type, $plugin . $class)) { - ${$class} =& new $class($options); + ${$class} =& new $class($settings); } elseif ($type === 'Model') { if ($plugin && class_exists($plugin .'AppModel')) { $appModel = $plugin .'AppModel'; } else { $appModel = 'AppModel'; } - ${$class} =& new $appModel($options); + ${$class} =& new $appModel(array_merge($settings, array('name' => $class))); } if (!isset(${$class})) { trigger_error(sprintf(__('(ClassRegistry::init() could not create instance of %1$s class %2$s ', true), $class, $type), E_USER_WARNING); return $false; + die(); } if ($type !== 'Model') { diff --git a/cake/libs/model/model.php b/cake/libs/model/model.php index ecfbf40d9..039aa93b1 100644 --- a/cake/libs/model/model.php +++ b/cake/libs/model/model.php @@ -315,7 +315,9 @@ class Model extends Overloadable { parent::__construct(); if (is_array($id)) { - extract(array_merge(array('id' => false, 'table' => null, 'ds' => null, 'name' => null, 'alias' => null), $id)); + extract(array_merge( + array('id' => $this->id, 'table' => $this->useTable, 'ds' => $this->useDbConfig, 'name' => $this->name, 'alias' => $this->alias), + $id)); $this->name = $name; $this->alias = $alias; } @@ -331,6 +333,7 @@ class Model extends Overloadable { if ($this->primaryKey === null) { $this->primaryKey = 'id'; } + ClassRegistry::addObject($this->alias, $this); $this->id = $id; diff --git a/cake/tests/cases/libs/class_registry.test.php b/cake/tests/cases/libs/class_registry.test.php index 1381493a5..40aa7700d 100644 --- a/cake/tests/cases/libs/class_registry.test.php +++ b/cake/tests/cases/libs/class_registry.test.php @@ -29,14 +29,14 @@ App::import('Core', 'ClassRegistry'); /** * ClassRegisterModel class - * + * * @package cake * @subpackage cake.tests.cases.libs */ class ClassRegisterModel extends CakeTestModel { /** * useTable property - * + * * @var bool false * @access public */ @@ -44,14 +44,14 @@ class ClassRegisterModel extends CakeTestModel { } /** * RegisterArticle class - * + * * @package cake * @subpackage cake.tests.cases.libs */ class RegisterArticle extends ClassRegisterModel { /** * name property - * + * * @var string 'RegisterArticle' * @access public */ @@ -59,14 +59,14 @@ class RegisterArticle extends ClassRegisterModel { } /** * RegisterArticleFeatured class - * + * * @package cake * @subpackage cake.tests.cases.libs */ class RegisterArticleFeatured extends ClassRegisterModel { /** * name property - * + * * @var string 'RegisterArticlFeatured' * @access public */ @@ -74,14 +74,14 @@ class RegisterArticleFeatured extends ClassRegisterModel { } /** * RegisterArticleTag class - * + * * @package cake * @subpackage cake.tests.cases.libs */ class RegisterArticleTag extends ClassRegisterModel { /** * name property - * + * * @var string 'RegisterArticlTag' * @access public */ @@ -89,14 +89,14 @@ class RegisterArticleTag extends ClassRegisterModel { } /** * RegistryPluginAppModel class - * + * * @package cake * @subpackage cake.tests.cases.libs */ class RegistryPluginAppModel extends ClassRegisterModel { /** * tablePrefix property - * + * * @var string 'something_' * @access public */ @@ -104,14 +104,14 @@ class RegistryPluginAppModel extends ClassRegisterModel { } /** * TestRegistryPluginModel class - * + * * @package cake * @subpackage cake.tests.cases.libs */ class TestRegistryPluginModel extends RegistryPluginAppModel { /** * name property - * + * * @var string 'TestRegistryPluginModel' * @access public */ @@ -119,14 +119,14 @@ class TestRegistryPluginModel extends RegistryPluginAppModel { } /** * ClassRegistryTest class - * + * * @package cake * @subpackage cake.tests.cases.libs */ class ClassRegistryTest extends CakeTestCase { /** * testAddModel method - * + * * @access public * @return void */ @@ -167,7 +167,7 @@ class ClassRegistryTest extends CakeTestCase { } /** * testClassRegistryFlush method - * + * * @access public * @return void */ @@ -182,7 +182,7 @@ class ClassRegistryTest extends CakeTestCase { } /** * testAddMultiplModels method - * + * * @access public * @return void */ @@ -223,7 +223,7 @@ class ClassRegistryTest extends CakeTestCase { } /** * testPluginAppModel method - * + * * @access public * @return void */ diff --git a/cake/tests/cases/libs/controller/controller.test.php b/cake/tests/cases/libs/controller/controller.test.php index d09b43f28..f2795696c 100644 --- a/cake/tests/cases/libs/controller/controller.test.php +++ b/cake/tests/cases/libs/controller/controller.test.php @@ -104,7 +104,7 @@ class ControllerComment extends CakeTestModel { * @var string 'ControllerComment' * @access public */ - var $name = 'ControllerComment'; + var $name = 'Comment'; /** * useTable property * @@ -253,6 +253,8 @@ class ControllerTest extends CakeTestCase { $Controller->constructClasses(); $this->assertTrue(is_a($Controller->ControllerPost, 'ControllerPost')); $this->assertTrue(is_a($Controller->ControllerComment, 'ControllerComment')); + + $this->assertEqual($Controller->ControllerComment->name, 'Comment'); unset($Controller); }