Skip to content
Permalink
Browse files

Lazy load the repository named after modelClass.

This means we don't have to require people to load the primary model, as
we can infer what it is based on the modelClass.
  • Loading branch information...
markstory committed Dec 6, 2013
1 parent fc37279 commit 9e4780a84870fe1d3d77cc62206585818b74b28c
Showing with 26 additions and 6 deletions.
  1. +7 −5 Cake/Controller/Controller.php
  2. +19 −1 Cake/Test/TestCase/Controller/ControllerTest.php
@@ -361,15 +361,15 @@ public function __construct($request = null, $response = null) {
* @param string $name
* @return boolean
*/
public function __isset($name) {
public function __get($name) {
if ($name === $this->modelClass) {
list($plugin, $class) = pluginSplit($name, true);
if (!$plugin) {
$plugin = $this->plugin ? $this->plugin . '.' : null;
}
return $this->repository($plugin . $this->modelClass);
$this->repository($plugin . $this->modelClass);
return $this->{$this->modelClass};
}
return false;
}
@@ -615,6 +615,10 @@ public function shutdownProcess() {
* @throws Cake\Error\MissingModelException if the model class cannot be found.
*/
public function repository($modelClass = null, $type = 'Table') {
if (isset($this->{$modelClass})) {
return $this->{$modelClass};
}
if ($modelClass === null) {
$modelClass = $this->modelClass;
}
@@ -623,8 +627,6 @@ public function repository($modelClass = null, $type = 'Table') {
if ($type === 'Table') {
$this->{$modelClass} = TableRegistry::get($plugin . $modelClass);
} else {
$this->{$modelClass} = $this->_loadRepository($plugin . $modelClass, $type);
}
if (!$this->{$modelClass}) {
throw new Error\MissingModelException($modelClass);
@@ -230,7 +230,25 @@ public function tearDown() {
}
/**
* testLoadModel method
* test autoload modelClass
*
* @return void
*/
public function testRepositoryAutoload() {
Configure::write('App.namespace', 'TestApp');
$request = new Request('controller_posts/index');
$response = $this->getMock('Cake\Network\Response');
$Controller = new Controller($request, $response);
$Controller->modelClass = 'Articles';
$this->assertInstanceOf(
'TestApp\Model\Repository\ArticlesTable',
$Controller->Articles
);
}
/**
* testRepository method
*
* @return void
*/

0 comments on commit 9e4780a

Please sign in to comment.
You can’t perform that action at this time.