Skip to content

Instantly share code, notes, and snippets.

@hurlatunde
Forked from egi/index.ctp
Created August 14, 2018 14:26
Show Gist options
  • Save hurlatunde/c46ca455212d9eefea8980f0e95eaf48 to your computer and use it in GitHub Desktop.
Save hurlatunde/c46ca455212d9eefea8980f0e95eaf48 to your computer and use it in GitHub Desktop.
CakePHP sample with custom query and pagination.
<?php pr($list); ?>
<?php
class User extends AppModel
{
public function paginate($conditions, $fields, $order, $limit, $page=1, $recursive=null, $extra=array())
{
$row_start = ($page-1) * $limit;
return $this->query("
SELECT User.*, SUM(Cutis.length)
FROM User
LEFT JOIN Cutis ON User.id=Cutis.user_id
GROUP BY User.id
LIMIT $row_start, $limit
");
}
public function paginateCount($conditions=0, $recursive=0, $extra=array())
{
$r = $this->query("
SELECT COUNT(User.id) FROM User
");
return $r['COUNT(User.id'];
}
}
<?php
class UserController extends AppController
{
var $uses = array('User');
var $components = array('Paginator');
function index()
{
$this->Paginator->paginate = array(
'limit' => 25
);
$this->set('list', $this->Paginator->paginate());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment