Skip to content

Commit

Permalink
Fix incorrect class being loaded by lazy model loading.
Browse files Browse the repository at this point in the history
We need to to correctly handle plugin prefixes in order to load the
correct class.

Refs #3689
  • Loading branch information
markstory committed Jun 11, 2014
1 parent b3098bd commit f91da49
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
9 changes: 3 additions & 6 deletions src/Controller/Controller.php
Expand Up @@ -311,12 +311,9 @@ public function addComponent($name, array $config = []) {
*/
public function __get($name) {
if (strpos($this->modelClass, $name) !== false) {
list($plugin, $class) = pluginSplit($name, true);
if (!$plugin) {
$plugin = $this->plugin ? $this->plugin . '.' : null;
}
$this->loadModel($plugin . $this->modelClass);
return $this->{$this->modelClass};
list($plugin, $class) = pluginSplit($this->modelClass, true);
$this->loadModel($plugin . $class);
return $this->{$class};
}
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Controller/ControllerTest.php
Expand Up @@ -312,7 +312,7 @@ public function testConstructSetModelClass() {
$request->params['plugin'] = 'TestPlugin';
$controller = new \TestPlugin\Controller\Admin\CommentsController($request, $response);
$this->assertEquals('TestPlugin.Comments', $controller->modelClass);
$this->assertInstanceOf('Cake\ORM\Table', $controller->Comments);
$this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $controller->Comments);
}

/**
Expand Down
25 changes: 25 additions & 0 deletions tests/test_app/Plugin/TestPlugin/src/Model/Table/CommentsTable.php
@@ -0,0 +1,25 @@
<?php
/**
* CakePHP : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakefoundation.org/projects/info/cakephp CakePHP Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace TestPlugin\Model\Table;

use Cake\ORM\Table;

/**
* Class CommentsTable
*
*/
class CommentsTable extends Table {

}

0 comments on commit f91da49

Please sign in to comment.