Skip to content

Commit

Permalink
Add finder options to Paginator->paginate method
Browse files Browse the repository at this point in the history
The paginator right now doesn't send $options to any finder, custom or otherwise. This change allows setting 'finderOptions' directly from the controller's paginate array.
  • Loading branch information
Patrick Conroy committed Aug 26, 2014
1 parent f13c341 commit 56d6f04
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Controller/Component/PaginatorComponent.php
Expand Up @@ -164,7 +164,7 @@ public function paginate($object, array $settings = []) {
unset($options['finder'], $options['maxLimit']);

if (empty($query)) {
$query = $object->find($type);
$query = $object->find($type, $options);
}

$query->applyOptions($options);
Expand Down
21 changes: 21 additions & 0 deletions tests/TestCase/Controller/Component/PaginatorComponentTest.php
Expand Up @@ -141,6 +141,27 @@ public function testPaginateExtraParams() {
$this->Paginator->paginate($table, $settings);
}

/**
* Test to make sure options get sent to custom finder methods via paginate
* @return void
*/
public function testPaginateCustomFinderOptions() {
$this->loadFixtures('Post');
$settings = [
'PaginatorPosts' => [
'finder' => 'author',
'author_id' => 1
]
];
$table = TableRegistry::get('PaginatorPosts');

$expected = $table->find('author', ['conditions' => ['PaginatorPosts.author_id' => $settings['PaginatorPosts']['author_id']]])
->count();
$result = $this->Paginator->paginate($table, $settings)->count();

$this->assertEquals($expected, $result);
}

/**
* Test that special paginate types are called and that the type param doesn't leak out into defaults or options.
*
Expand Down
14 changes: 14 additions & 0 deletions tests/test_app/TestApp/Model/Table/PaginatorPostsTable.php
Expand Up @@ -52,4 +52,18 @@ public function findPublished(Query $query, array $options) {
return $query;
}

/**
* Custom finder, used with fixture data to ensure Paginator is sending options
*
* @param Cake\ORM\Query $query
* @param array $options
* @return Cake\ORM\Query
*/
public function findAuthor(Query $query, array $options = []) {
if (isset($options['author_id'])) {
$query->where(['PaginatorPosts.author_id' => $options['author_id']]);
}
return $query;
}

}

0 comments on commit 56d6f04

Please sign in to comment.