Removing the Controller::modelNames property and loading it's contents in the uses variable.

Simplifying code in Controller::render()
Setting all models loaded into the request parameters so it can be used later on the Form helper
This commit is contained in:
Jose Lorenzo Rodriguez 2011-07-12 01:19:14 -04:30
parent f8ab1d3c73
commit e45b35cffe
2 changed files with 32 additions and 36 deletions

View file

@ -127,12 +127,6 @@ class Controller extends Object {
*/
public $viewVars = array();
/**
* An array containing the class names of the models this controller uses.
*
* @var array Array of model objects.
*/
public $modelNames = array();
/**
* The name of the view file to render. The name specified
@ -596,9 +590,13 @@ class Controller extends Object {
if ($modelClass === null) {
$modelClass = $this->modelClass;
}
list($plugin, $modelClass) = pluginSplit($modelClass, true);
$this->modelNames[] = $modelClass;
$this->uses = ($this->uses) ? $this->uses : array();
if (!in_array($modelClass, $this->uses)) {
$this->uses[] = $modelClass;
}
list($plugin, $modelClass) = pluginSplit($modelClass, true);
$this->{$modelClass} = ClassRegistry::init(array(
'class' => $plugin . $modelClass, 'alias' => $modelClass, 'id' => $id
@ -806,36 +804,25 @@ class Controller extends Object {
App::uses($viewClass, $plugin . 'View');
}
$this->request->params['models'] = $this->modelNames;
$View = new $viewClass($this);
if (!empty($this->modelNames)) {
$models = array();
foreach ($this->modelNames as $currentModel) {
if (isset($this->$currentModel) && is_a($this->$currentModel, 'Model')) {
$models[] = Inflector::underscore($currentModel);
}
$isValidModel = (
isset($this->$currentModel) && is_a($this->$currentModel, 'Model') &&
!empty($this->$currentModel->validationErrors)
);
if ($isValidModel) {
$View->validationErrors[Inflector::camelize($currentModel)] =&
$this->$currentModel->validationErrors;
if (!empty($this->uses)) {
foreach ($this->uses as $model) {
list($plugin, $model) = pluginSplit($model);
$this->request->params['models'][$model] = $plugin;
}
}
$models = array_diff(ClassRegistry::keys(), $models);
$models = ClassRegistry::keys();
foreach ($models as $currentModel) {
if (ClassRegistry::isKeySet($currentModel)) {
$currentObject = ClassRegistry::getObject($currentModel);
if (is_a($currentObject, 'Model') && !empty($currentObject->validationErrors)) {
$View->validationErrors[Inflector::camelize($currentModel)] =&
if (is_a($currentObject, 'Model')) {
list($plugin, $package) = pluginSplit(App::location(get_class($currentObject)));
$this->request->params['models'][$currentObject->alias] = $plugin;
$View->validationErrors[$currentObject->alias] =&
$currentObject->validationErrors;
}
}
}
}
$this->autoRender = false;
$this->View = $View;

View file

@ -425,7 +425,7 @@ class ControllerTest extends CakeTestCase {
$result = $Controller->loadModel('ControllerPost');
$this->assertTrue($result);
$this->assertTrue(is_a($Controller->ControllerPost, 'ControllerPost'));
$this->assertTrue(in_array('ControllerPost', $Controller->modelNames));
$this->assertTrue(in_array('ControllerPost', $Controller->uses));
ClassRegistry::flush();
unset($Controller);
@ -456,7 +456,7 @@ class ControllerTest extends CakeTestCase {
$result = $Controller->loadModel('Comment');
$this->assertTrue($result);
$this->assertInstanceOf('Comment', $Controller->Comment);
$this->assertTrue(in_array('Comment', $Controller->modelNames));
$this->assertTrue(in_array('Comment', $Controller->uses));
ClassRegistry::flush();
unset($Controller);
@ -613,6 +613,7 @@ class ControllerTest extends CakeTestCase {
App::build(array(
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View'. DS)
), true);
ClassRegistry::flush();
$request = new CakeRequest('controller_posts/index');
$request->params['action'] = 'index';
@ -636,13 +637,20 @@ class ControllerTest extends CakeTestCase {
$Controller->ControllerComment->validationErrors = array('title' => 'tooShort');
$expected = $Controller->ControllerComment->validationErrors;
ClassRegistry::flush();
$Controller->viewPath = 'Posts';
$result = $Controller->render('index');
$View = $Controller->View;
$this->assertTrue(isset($View->validationErrors['ControllerComment']));
$this->assertEqual($expected, $View->validationErrors['ControllerComment']);
$expectedModels = array(
'ControllerAlias' => null,
'ControllerComment' => null,
'ControllerPost' => null
);
$this->assertEqual($expectedModels, $Controller->request->params['models']);
$Controller->ControllerComment->validationErrors = array();
ClassRegistry::flush();
@ -998,6 +1006,7 @@ class ControllerTest extends CakeTestCase {
* @return void
*/
public function testValidateErrors() {
ClassRegistry::flush();
$request = new CakeRequest('controller_posts/index');
$TestController = new TestController($request);