From f91da49a086769e28c454d5bc2b05d2f23f3648f Mon Sep 17 00:00:00 2001 From: mark_story Date: Wed, 11 Jun 2014 10:14:25 -0400 Subject: [PATCH] Fix incorrect class being loaded by lazy model loading. We need to to correctly handle plugin prefixes in order to load the correct class. Refs #3689 --- src/Controller/Controller.php | 9 +++---- tests/TestCase/Controller/ControllerTest.php | 2 +- .../src/Model/Table/CommentsTable.php | 25 +++++++++++++++++++ 3 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 tests/test_app/Plugin/TestPlugin/src/Model/Table/CommentsTable.php diff --git a/src/Controller/Controller.php b/src/Controller/Controller.php index 94bd6f77fac..3071bc1fedf 100644 --- a/src/Controller/Controller.php +++ b/src/Controller/Controller.php @@ -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; } diff --git a/tests/TestCase/Controller/ControllerTest.php b/tests/TestCase/Controller/ControllerTest.php index f928093263f..aad4d1a4108 100644 --- a/tests/TestCase/Controller/ControllerTest.php +++ b/tests/TestCase/Controller/ControllerTest.php @@ -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); } /** diff --git a/tests/test_app/Plugin/TestPlugin/src/Model/Table/CommentsTable.php b/tests/test_app/Plugin/TestPlugin/src/Model/Table/CommentsTable.php new file mode 100644 index 00000000000..a2293b3b781 --- /dev/null +++ b/tests/test_app/Plugin/TestPlugin/src/Model/Table/CommentsTable.php @@ -0,0 +1,25 @@ +