From 62bf371aec7eca606e3e8bbbe912549c18554d3d Mon Sep 17 00:00:00 2001 From: "mariano.iglesias" Date: Thu, 22 May 2008 17:07:42 +0000 Subject: [PATCH] Allowing paginate to override the default find type for fetching records. Set the find type as the first string parameter in your paginate settings: var $paginate = array('popular', ...); Check the test case for an example. git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7013 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/controller/controller.php | 7 +++++-- .../tests/cases/libs/controller/controller.test.php | 13 +++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/cake/libs/controller/controller.php b/cake/libs/controller/controller.php index 1a194e80c..675217a68 100644 --- a/cake/libs/controller/controller.php +++ b/cake/libs/controller/controller.php @@ -978,7 +978,10 @@ class Controller extends Object { $conditions = array($conditions, $scope); } $recursive = $object->recursive; - + $type = 'all'; + if (isset($defaults[0])) { + $type = array_shift($defaults); + } $extra = array_diff_key($defaults, compact('conditions', 'fields', 'order', 'limit', 'page', 'recursive')); if (method_exists($object, 'paginateCount')) { $count = $object->paginateCount($conditions, $recursive); @@ -996,7 +999,7 @@ class Controller extends Object { if (method_exists($object, 'paginate')) { $results = $object->paginate($conditions, $fields, $order, $limit, $page, $recursive); } else { - $results = $object->find('all', array_merge(compact('conditions', 'fields', 'order', 'limit', 'page', 'recursive'), $extra)); + $results = $object->find($type, array_merge(compact('conditions', 'fields', 'order', 'limit', 'page', 'recursive'), $extra)); } $paging = array( 'page' => $page, diff --git a/cake/tests/cases/libs/controller/controller.test.php b/cake/tests/cases/libs/controller/controller.test.php index d5c72124e..b33073c22 100644 --- a/cake/tests/cases/libs/controller/controller.test.php +++ b/cake/tests/cases/libs/controller/controller.test.php @@ -39,6 +39,14 @@ class ControllerPost extends CakeTestModel { function beforeFind($query) { $this->lastQuery = $query; } + + function find($type, $options = array()) { + if ($type == 'popular') { + $conditions = array($this->name . '.' . $this->primaryKey => '> 1'); + return parent::find('all', Set::merge($options, compact('conditions'))); + } + return parent::find($type, $options); + } } class ControllerComment extends CakeTestModel { var $name = 'ControllerComment'; @@ -156,6 +164,11 @@ class ControllerTest extends CakeTestCase { $this->assertEqual($Controller->params['paging']['ControllerPost']['page'], 1); $this->assertEqual(Set::extract($result, '{n}.ControllerPost.id'), array(1, 2, 3)); $this->assertFalse(!isset($Controller->ControllerPost->lastQuery['contain'])); + + $Controller->paginate = array('ControllerPost' => array('popular', 'fields' => array('id', 'title'))); + $result = $Controller->paginate('ControllerPost'); + $this->assertEqual(Set::extract($result, '{n}.ControllerPost.id'), array(2, 3)); + $this->assertEqual($Controller->ControllerPost->lastQuery['conditions'], array('ControllerPost.id' => '> 1')); } function testDefaultPaginateParams() {