mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 11:28:25 +00:00
Lazy loading models in controllers, now declaring multiple models in $uses is not a performance hit anymore
This commit is contained in:
parent
3433d123d4
commit
8da7e5fa82
3 changed files with 31 additions and 15 deletions
|
@ -336,6 +336,7 @@ class Controller extends Object {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides backwards compatibility to avoid problems with empty and isset to alias properties.
|
* Provides backwards compatibility to avoid problems with empty and isset to alias properties.
|
||||||
|
* Lazy loads models using the loadModel() method if declared in $uses
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
|
@ -349,6 +350,27 @@ class Controller extends Object {
|
||||||
case 'params':
|
case 'params':
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (is_array($this->uses)) {
|
||||||
|
foreach ($this->uses as $modelClass) {
|
||||||
|
list($plugin, $class) = pluginSplit($modelClass, true);
|
||||||
|
if ($name === $class) {
|
||||||
|
if (!$plugin) {
|
||||||
|
$plugin = $this->plugin ? $this->plugin . '.' : null;
|
||||||
|
}
|
||||||
|
return $this->loadModel($modelClass);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($name === $this->modelClass) {
|
||||||
|
list($plugin, $class) = pluginSplit($name, true);
|
||||||
|
if (!$plugin) {
|
||||||
|
$plugin = $this->plugin ? $this->plugin . '.' : null;
|
||||||
|
}
|
||||||
|
return $this->loadModel($plugin . $this->modelClass);
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -372,6 +394,11 @@ class Controller extends Object {
|
||||||
case 'paginate':
|
case 'paginate':
|
||||||
return $this->Components->load('Paginator')->settings;
|
return $this->Components->load('Paginator')->settings;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isset($this->{$name})) {
|
||||||
|
return $this->{$name};
|
||||||
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -493,19 +520,9 @@ class Controller extends Object {
|
||||||
public function constructClasses() {
|
public function constructClasses() {
|
||||||
$this->__mergeVars();
|
$this->__mergeVars();
|
||||||
$this->Components->init($this);
|
$this->Components->init($this);
|
||||||
|
if ($this->uses) {
|
||||||
if ($this->uses !== null || ($this->uses !== array())) {
|
$this->uses = $uses = is_array($this->uses) ? $this->uses : array($this->uses);
|
||||||
$plugin = $this->plugin ? $this->plugin . '.' : null;
|
list(, $this->modelClass) = pluginSplit($uses[0]);
|
||||||
if ($this->uses === false) {
|
|
||||||
$this->loadModel($plugin . $this->modelClass);
|
|
||||||
} elseif ($this->uses) {
|
|
||||||
$uses = is_array($this->uses) ? $this->uses : array($this->uses);
|
|
||||||
list($plugin, $modelClassName) = pluginSplit($uses[0]);
|
|
||||||
$this->modelClass = $modelClassName;
|
|
||||||
foreach ($uses as $modelClass) {
|
|
||||||
$this->loadModel($modelClass);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -486,7 +486,6 @@ class ControllerTest extends CakeTestCase {
|
||||||
$Controller->uses = array('TestPlugin.TestPluginPost');
|
$Controller->uses = array('TestPlugin.TestPluginPost');
|
||||||
$Controller->constructClasses();
|
$Controller->constructClasses();
|
||||||
|
|
||||||
$this->assertEqual($Controller->modelClass, 'TestPluginPost');
|
|
||||||
$this->assertTrue(isset($Controller->TestPluginPost));
|
$this->assertTrue(isset($Controller->TestPluginPost));
|
||||||
$this->assertTrue(is_a($Controller->TestPluginPost, 'TestPluginPost'));
|
$this->assertTrue(is_a($Controller->TestPluginPost, 'TestPluginPost'));
|
||||||
|
|
||||||
|
@ -909,7 +908,7 @@ class ControllerTest extends CakeTestCase {
|
||||||
$this->assertTrue(in_array('ControllerPost', $appVars['uses']));
|
$this->assertTrue(in_array('ControllerPost', $appVars['uses']));
|
||||||
$this->assertNull($testVars['uses']);
|
$this->assertNull($testVars['uses']);
|
||||||
|
|
||||||
$this->assertFalse(isset($TestController->ControllerPost));
|
$this->assertFalse(property_exists($TestController, 'ControllerPost'));
|
||||||
|
|
||||||
|
|
||||||
$TestController = new ControllerCommentsController($request);
|
$TestController = new ControllerCommentsController($request);
|
||||||
|
|
Loading…
Reference in a new issue