From 61eebea0f44e9036ef2be202239c6f75009e978f Mon Sep 17 00:00:00 2001 From: phpnut Date: Sun, 2 Dec 2007 22:58:23 +0000 Subject: [PATCH] "Refactoring Controller::paginate()" git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6105 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/controller/controller.php | 34 +++++++++---------- .../cases/libs/controller/controller.test.php | 6 ++++ 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/cake/libs/controller/controller.php b/cake/libs/controller/controller.php index 67eadce1f..6075696ab 100644 --- a/cake/libs/controller/controller.php +++ b/cake/libs/controller/controller.php @@ -853,34 +853,34 @@ class Controller extends Object { } $assoc = null; - if (is_string($object) && !strpos($object, '.')) { - if (isset($this->{$object})) { + if (is_string($object)) { + $assoc = null; + + if (strpos($object, '.') !== false) { + list($object, $assoc) = explode('.', $object); + } + + if ($assoc && isset($this->{$object}->{$assoc})) { + $object = $this->{$object}->{$assoc}; + } elseif ($assoc && isset($this->{$this->modelClass}) && isset($this->{$this->modelClass}->{$assoc})) { + $object = $this->{$this->modelClass}->{$assoc}; + } elseif (isset($this->{$object})) { $object = $this->{$object}; } elseif (isset($this->{$this->modelClass}) && isset($this->{$this->modelClass}->{$object})) { $object = $this->{$this->modelClass}->{$object}; - } elseif (!empty($this->uses)) { - for ($i = 0; $i < count($this->uses); $i++) { - $model = $this->uses[$i]; - if (isset($this->{$model}->{$object})) { - $object = $this->{$model}->{$object}; - break; - } - } - } - } elseif (is_string($object)) { - list($object, $assoc) = explode('.', $object); - if (isset($this->{$object})) { - $object = $this->{$object}; } } elseif (empty($object) || $object == null) { if (isset($this->{$this->modelClass})) { $object = $this->{$this->modelClass}; } else { + $className = null; if (strpos($this->uses[0], '.') !== false) { - list($plugin, $className) = explode('.', $this->uses[0]); + list($name, $className) = explode('.', $this->uses[0]); + } + if ($className) { $object = $this->{$className}; } else { - $object = $this->{$this->uses[0]}; + $object = $this->{$name}; } } } diff --git a/cake/tests/cases/libs/controller/controller.test.php b/cake/tests/cases/libs/controller/controller.test.php index cd0b46303..0f8534c57 100644 --- a/cake/tests/cases/libs/controller/controller.test.php +++ b/cake/tests/cases/libs/controller/controller.test.php @@ -86,6 +86,12 @@ class ControllerTest extends CakeTestCase { $Controller->constructClasses(); $Controller->modelClass = null; + $results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id'); + $this->assertEqual($results, array(1, 2, 3)); + + $results = Set::extract($Controller->paginate('ControllerComment'), '{n}.ControllerComment.id'); + $this->assertEqual($results, array(1, 2, 3, 4, 5, 6)); + $Controller->uses[0] = 'Plugin.ControllerPost'; $results = Set::extract($Controller->paginate(), '{n}.ControllerPost.id'); $this->assertEqual($results, array(1, 2, 3));