Skip to content

Commit

Permalink
Better custom find for pagination
Browse files Browse the repository at this point in the history
Instead of shuffling the paginator settings you can now simply add a new "findType" key and it will automatically change the find() type accordingly
  • Loading branch information
jippi committed Jul 19, 2012
1 parent 3c6b509 commit bce82a2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/Cake/Controller/Component/PaginatorComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ public function paginate($object = null, $scope = array(), $whitelist = array())
$extra = array_diff_key($options, compact(
'conditions', 'fields', 'order', 'limit', 'page', 'recursive'
));

if (!empty($extra['findType'])) {
$type = $extra['findType'];
unset($extra['findType']);
}

if ($type !== 'all') {
$extra['type'] = $type;
}
Expand Down
54 changes: 54 additions & 0 deletions lib/Cake/Test/Case/Controller/Component/PaginatorComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,28 @@ public function testMergeOptionsNamedParams() {
$this->assertEquals($expected, $result);
}

/**
* test mergeOptions with customFind key
*
* @return void
*/
public function testMergeOptionsCustomFindKey() {
$this->request->params['named'] = array(
'page' => 10,
'limit' => 10
);
$this->Paginator->settings = array(
'page' => 1,
'limit' => 20,
'maxLimit' => 100,
'paramType' => 'named',
'findType' => 'myCustomFind'
);
$result = $this->Paginator->mergeOptions('Post');
$expected = array('page' => 10, 'limit' => 10, 'maxLimit' => 100, 'paramType' => 'named', 'findType' => 'myCustomFind');
$this->assertEquals($expected, $result);
}

/**
* test merging options from the querystring.
*
Expand Down Expand Up @@ -1078,6 +1100,38 @@ public function testPaginateCustomFindFieldsArray() {
$this->assertTrue($result['nextPage']);
$this->assertFalse($result['prevPage']);
}
/**
* test paginate() and custom find with customFind key, to make sure the correct count is returned.
*
* @return void
*/
public function testPaginateCustomFindWithCustomFindKey() {
$Controller =& new Controller($this->request);
$Controller->uses = array('PaginatorCustomPost');
$Controller->constructClasses();
$data = array('author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
$Controller->PaginatorCustomPost->create($data);
$result = $Controller->PaginatorCustomPost->save();
$this->assertTrue(!empty($result));

$Controller->paginate = array(
'conditions' => array('PaginatorCustomPost.published' => 'Y'),
'findType' => 'list',
'limit' => 2
);
$result = $Controller->paginate();
$expected = array(
1 => 'First Post',
2 => 'Second Post',
);
$this->assertEquals($expected, $result);
$result = $Controller->params['paging']['PaginatorCustomPost'];
$this->assertEquals(2, $result['current']);
$this->assertEquals(3, $result['count']);
$this->assertEquals(2, $result['pageCount']);
$this->assertTrue($result['nextPage']);
$this->assertFalse($result['prevPage']);
}

/**
* test paginate() and custom find with fields array, to make sure the correct count is returned.
Expand Down

0 comments on commit bce82a2

Please sign in to comment.