Created
March 22, 2014 23:54
-
-
Save jaggy/9716263 to your computer and use it in GitHub Desktop.
CakePHP paging behavior to complement with my REST plug-in
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class PageableBehavior extends ModelBehavior | |
{ | |
/** | |
* A custom paginate | |
* | |
* @group scope | |
* @param Model $model | |
* @param integer $page | |
* @param array $options | |
* @return array | |
*/ | |
function paginate( Model $model, $page, $options = array() ) | |
{ | |
// default limit count if not overidden | |
$limit = (array_key_exists( 'limit', $options )) ? $options[ 'limit' ] : 10; | |
// generate the offset | |
// page-1 * limit | |
$offset = ($page-1) * $limit; | |
$defaults = [ | |
'offset' => $offset | |
]; | |
// override the defaults | |
$options = array_merge( $options, $defaults ); | |
$data = $model->find( 'all', $options); | |
return $data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment