Skip to content

Instantly share code, notes, and snippets.

@jaggy
Created March 22, 2014 23:54
Show Gist options
  • Save jaggy/9716263 to your computer and use it in GitHub Desktop.
Save jaggy/9716263 to your computer and use it in GitHub Desktop.
CakePHP paging behavior to complement with my REST plug-in
<?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