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
This commit is contained in:
gwoo 2008-08-25 22:33:36 +00:00
parent 4b3a5f5df0
commit e35a215883
4 changed files with 32 additions and 31 deletions

View file

@ -106,49 +106,45 @@ class ClassRegistry {
$objects = array(array('class' => $class)); $objects = array(array('class' => $class));
} }
$defaults = array_merge( $defaults = isset($_this->__config[$type]) ? $_this->__config[$type] : array();
array('id' => false, 'table' => null, 'ds' => null, 'alias' => null, 'name' => null),
isset($_this->__config[$type]) ? $_this->__config[$type] : array()
);
$count = count($objects); $count = count($objects);
foreach ($objects as $key => $settings) { foreach ($objects as $key => $settings) {
if (is_array($settings)) { if (is_array($settings)) {
$plugin = null; $plugin = null;
$settings = array_merge($defaults, $settings); $settings = array_merge($defaults, $settings);
extract($settings, EXTR_OVERWRITE);
$class = $settings['class'];
if (strpos($class, '.') !== false) { if (strpos($class, '.') !== false) {
list($plugin, $class) = explode('.', $class); list($plugin, $class) = explode('.', $class);
$plugin = $plugin . '.'; $plugin = $plugin . '.';
} }
if (empty($alias)) { if (empty($settings['alias'])) {
$alias = $class; $settings['alias'] = $class;
} }
$alias = $settings['alias'];
if ($model =& $_this->_duplicate($alias, $class)) { if ($model =& $_this->_duplicate($alias, $class)) {
$_this->map($alias, $class); $_this->map($alias, $class);
return $model; return $model;
} }
if ($type === 'Model') {
$options = array('id' => $id, 'table' => $table, 'ds' => $ds, 'alias' => $alias, 'name' => $class);
}
if (App::import($type, $plugin . $class)) { if (App::import($type, $plugin . $class)) {
${$class} =& new $class($options); ${$class} =& new $class($settings);
} elseif ($type === 'Model') { } elseif ($type === 'Model') {
if ($plugin && class_exists($plugin .'AppModel')) { if ($plugin && class_exists($plugin .'AppModel')) {
$appModel = $plugin .'AppModel'; $appModel = $plugin .'AppModel';
} else { } else {
$appModel = 'AppModel'; $appModel = 'AppModel';
} }
${$class} =& new $appModel($options); ${$class} =& new $appModel(array_merge($settings, array('name' => $class)));
} }
if (!isset(${$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); trigger_error(sprintf(__('(ClassRegistry::init() could not create instance of %1$s class %2$s ', true), $class, $type), E_USER_WARNING);
return $false; return $false;
die();
} }
if ($type !== 'Model') { if ($type !== 'Model') {

View file

@ -315,7 +315,9 @@ class Model extends Overloadable {
parent::__construct(); parent::__construct();
if (is_array($id)) { 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->name = $name;
$this->alias = $alias; $this->alias = $alias;
} }
@ -331,6 +333,7 @@ class Model extends Overloadable {
if ($this->primaryKey === null) { if ($this->primaryKey === null) {
$this->primaryKey = 'id'; $this->primaryKey = 'id';
} }
ClassRegistry::addObject($this->alias, $this); ClassRegistry::addObject($this->alias, $this);
$this->id = $id; $this->id = $id;

View file

@ -104,7 +104,7 @@ class ControllerComment extends CakeTestModel {
* @var string 'ControllerComment' * @var string 'ControllerComment'
* @access public * @access public
*/ */
var $name = 'ControllerComment'; var $name = 'Comment';
/** /**
* useTable property * useTable property
* *
@ -254,6 +254,8 @@ class ControllerTest extends CakeTestCase {
$this->assertTrue(is_a($Controller->ControllerPost, 'ControllerPost')); $this->assertTrue(is_a($Controller->ControllerPost, 'ControllerPost'));
$this->assertTrue(is_a($Controller->ControllerComment, 'ControllerComment')); $this->assertTrue(is_a($Controller->ControllerComment, 'ControllerComment'));
$this->assertEqual($Controller->ControllerComment->name, 'Comment');
unset($Controller); unset($Controller);
} }
/** /**