Skip to content

Commit

Permalink
Also cleaned up a bit pagination code
Browse files Browse the repository at this point in the history
This removes the option of passing the finder name as an array value
in the pagination options. I think that looked like an error instead
  • Loading branch information
lorenzo committed Sep 6, 2014
1 parent 36ba3bb commit 0c605c8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
36 changes: 27 additions & 9 deletions src/Controller/Component/PaginatorComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,10 @@ public function paginate($object, array $settings = []) {

$options += ['page' => 1];
$options['page'] = intval($options['page']) < 1 ? 1 : (int)$options['page'];

if (!isset($options['finder']) && isset($options['findType'])) {
trigger_error('You should use finder instead of findType', E_USER_DEPRECATED);
$options['finder'] = $options['findType'];
}
$type = !empty($options['finder']) ? $options['finder'] : 'all';
unset($options['finder'], $options['maxLimit']);
list($finder, $options) = $this->_extractFinder($options);

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

$query->applyOptions($options);
Expand All @@ -190,7 +184,7 @@ public function paginate($object, array $settings = []) {
}

$paging = array(
'finder' => $type,
'finder' => $finder,
'page' => $page,
'current' => $numResults,
'count' => $count,
Expand Down Expand Up @@ -220,6 +214,30 @@ public function paginate($object, array $settings = []) {
return $results;
}

/**
* Extracts the finder name and options out of the provided pagination options
*
* @param array $options the pagination options
* @return array An array containing in the first position the finder name and
* in the second the options to be passed to it
*/
protected function _extractFinder($options) {
if (!isset($options['finder']) && isset($options['findType'])) {
trigger_error('You should use finder instead of findType', E_USER_DEPRECATED);
$options['finder'] = $options['findType'];
}

$type = !empty($options['finder']) ? $options['finder'] : 'all';
unset($options['finder'], $options['maxLimit']);

if (is_array($type)) {
$options = $options + (array)current($type);
$type = key($type);
}

return [$type, $options];
}

/**
* Merges the various options that Pagination uses.
* Pulls settings together from the following places:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,12 @@ public function testPaginateCustomFinderOptions() {
];
$table = TableRegistry::get('PaginatorPosts');

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

Expand Down

0 comments on commit 0c605c8

Please sign in to comment.