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
This commit is contained in:
mariano.iglesias 2008-05-22 17:07:42 +00:00
parent aaab0306aa
commit 62bf371aec
2 changed files with 18 additions and 2 deletions

View file

@ -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,

View file

@ -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() {