Skip to content

Commit

Permalink
Changed the paginator to do a count after the find.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbasso committed Jul 24, 2011
1 parent 6fb3c72 commit b8c00d5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 34 deletions.
28 changes: 13 additions & 15 deletions lib/Cake/Controller/Component/PaginatorComponent.php
Expand Up @@ -153,21 +153,8 @@ public function paginate($object = null, $scope = array(), $whitelist = array())
$extra['type'] = $type; $extra['type'] = $type;
} }


if ($object->hasMethod('paginateCount')) { if (intval($page) < 1) {
$count = $object->paginateCount($conditions, $recursive, $extra); $page = 1;
} else {
$parameters = compact('conditions');
if ($recursive != $object->recursive) {
$parameters['recursive'] = $recursive;
}
$count = $object->find('count', array_merge($parameters, $extra));
}
$pageCount = intval(ceil($count / $limit));

if ($page === 'last' || $page >= $pageCount) {
$options['page'] = $page = $pageCount;
} elseif (intval($page) < 1) {
$options['page'] = $page = 1;
} }
$page = $options['page'] = (int)$page; $page = $options['page'] = (int)$page;


Expand All @@ -185,6 +172,17 @@ public function paginate($object = null, $scope = array(), $whitelist = array())
$defaults = $this->getDefaults($object->alias); $defaults = $this->getDefaults($object->alias);
unset($defaults[0]); unset($defaults[0]);


if ($object->hasMethod('paginateCount')) {
$count = $object->paginateCount($conditions, $recursive, $extra);
} else {
$parameters = compact('conditions');
if ($recursive != $object->recursive) {
$parameters['recursive'] = $recursive;
}
$count = $object->find('count', array_merge($parameters, $extra));
}
$pageCount = intval(ceil($count / $limit));

$paging = array( $paging = array(
'page' => $page, 'page' => $page,
'current' => count($results), 'current' => count($results),
Expand Down
37 changes: 18 additions & 19 deletions lib/Cake/Test/Case/Controller/Component/PaginatorComponentTest.php
Expand Up @@ -87,12 +87,11 @@ class PaginatorControllerPost extends CakeTestModel {
public $invalidFields = array('name' => 'error_msg'); public $invalidFields = array('name' => 'error_msg');


/** /**
* lastQuery property * lastQueries property
* *
* @var mixed null * @var array
* @access public
*/ */
public $lastQuery = null; public $lastQueries = array();


/** /**
* beforeFind method * beforeFind method
Expand All @@ -102,7 +101,7 @@ class PaginatorControllerPost extends CakeTestModel {
* @return void * @return void
*/ */
public function beforeFind($query) { public function beforeFind($query) {
$this->lastQuery = $query; array_unshift($this->lastQueries, $query);
} }


/** /**
Expand Down Expand Up @@ -278,7 +277,7 @@ public function testPaginate() {
'sort' => 'PaginatorControllerPost.author_id', 'direction' => 'allYourBase' 'sort' => 'PaginatorControllerPost.author_id', 'direction' => 'allYourBase'
); );
$results = Set::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id'); $results = Set::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
$this->assertEqual($Controller->PaginatorControllerPost->lastQuery['order'][0], array('PaginatorControllerPost.author_id' => 'asc')); $this->assertEqual($Controller->PaginatorControllerPost->lastQueries[1]['order'][0], array('PaginatorControllerPost.author_id' => 'asc'));
$this->assertEqual($results, array(1, 3, 2)); $this->assertEqual($results, array(1, 3, 2));


$Controller->request->params['named'] = array(); $Controller->request->params['named'] = array();
Expand Down Expand Up @@ -316,22 +315,22 @@ public function testPaginate() {
public function testPageParamCasting() { public function testPageParamCasting() {
$this->Controller->Post->expects($this->at(0)) $this->Controller->Post->expects($this->at(0))
->method('hasMethod') ->method('hasMethod')
->with('paginateCount') ->with('paginate')
->will($this->returnValue(false)); ->will($this->returnValue(false));

$this->Controller->Post->expects($this->at(1)) $this->Controller->Post->expects($this->at(1))
->method('find') ->method('find')
->will($this->returnValue(2)); ->will($this->returnValue(array('stuff')));

$this->Controller->Post->expects($this->at(2)) $this->Controller->Post->expects($this->at(2))
->method('hasMethod') ->method('hasMethod')
->with('paginate') ->with('paginateCount')
->will($this->returnValue(false)); ->will($this->returnValue(false));

$this->Controller->Post->expects($this->at(3)) $this->Controller->Post->expects($this->at(3))
->method('find') ->method('find')
->will($this->returnValue(array('stuff'))); ->will($this->returnValue(2));

$this->request->params['named'] = array('page' => '1 " onclick="alert(\'xss\');">'); $this->request->params['named'] = array('page' => '1 " onclick="alert(\'xss\');">');
$this->Paginator->settings = array('limit' => 1, 'maxLimit' => 10, 'paramType' => 'named'); $this->Paginator->settings = array('limit' => 1, 'maxLimit' => 10, 'paramType' => 'named');
$this->Paginator->paginate('Post'); $this->Paginator->paginate('Post');
Expand All @@ -356,7 +355,7 @@ public function testPaginateExtraParams() {
$result = $Controller->Paginator->paginate('PaginatorControllerPost'); $result = $Controller->Paginator->paginate('PaginatorControllerPost');
$this->assertEqual($Controller->params['paging']['PaginatorControllerPost']['page'], 1); $this->assertEqual($Controller->params['paging']['PaginatorControllerPost']['page'], 1);
$this->assertEqual(Set::extract($result, '{n}.PaginatorControllerPost.id'), array(1, 2, 3)); $this->assertEqual(Set::extract($result, '{n}.PaginatorControllerPost.id'), array(1, 2, 3));
$this->assertTrue(!isset($Controller->PaginatorControllerPost->lastQuery['contain'])); $this->assertTrue(!isset($Controller->PaginatorControllerPost->lastQueries[1]['contain']));


$Controller->request->params['named'] = array('page' => '-1'); $Controller->request->params['named'] = array('page' => '-1');
$Controller->Paginator->settings = array( $Controller->Paginator->settings = array(
Expand All @@ -369,7 +368,7 @@ public function testPaginateExtraParams() {
$result = $Controller->Paginator->paginate('PaginatorControllerPost'); $result = $Controller->Paginator->paginate('PaginatorControllerPost');
$this->assertEqual($Controller->params['paging']['PaginatorControllerPost']['page'], 1); $this->assertEqual($Controller->params['paging']['PaginatorControllerPost']['page'], 1);
$this->assertEqual(Set::extract($result, '{n}.PaginatorControllerPost.id'), array(1, 2, 3)); $this->assertEqual(Set::extract($result, '{n}.PaginatorControllerPost.id'), array(1, 2, 3));
$this->assertTrue(isset($Controller->PaginatorControllerPost->lastQuery['contain'])); $this->assertTrue(isset($Controller->PaginatorControllerPost->lastQueries[1]['contain']));


$Controller->Paginator->settings = array( $Controller->Paginator->settings = array(
'PaginatorControllerPost' => array( 'PaginatorControllerPost' => array(
Expand All @@ -378,14 +377,14 @@ public function testPaginateExtraParams() {
); );
$result = $Controller->Paginator->paginate('PaginatorControllerPost'); $result = $Controller->Paginator->paginate('PaginatorControllerPost');
$this->assertEqual(Set::extract($result, '{n}.PaginatorControllerPost.id'), array(2, 3)); $this->assertEqual(Set::extract($result, '{n}.PaginatorControllerPost.id'), array(2, 3));
$this->assertEqual($Controller->PaginatorControllerPost->lastQuery['conditions'], array('PaginatorControllerPost.id > ' => '1')); $this->assertEqual($Controller->PaginatorControllerPost->lastQueries[1]['conditions'], array('PaginatorControllerPost.id > ' => '1'));


$Controller->request->params['named'] = array('limit' => 12); $Controller->request->params['named'] = array('limit' => 12);
$Controller->Paginator->settings = array('limit' => 30, 'maxLimit' => 100, 'paramType' => 'named'); $Controller->Paginator->settings = array('limit' => 30, 'maxLimit' => 100, 'paramType' => 'named');
$result = $Controller->Paginator->paginate('PaginatorControllerPost'); $result = $Controller->Paginator->paginate('PaginatorControllerPost');
$paging = $Controller->params['paging']['PaginatorControllerPost']; $paging = $Controller->params['paging']['PaginatorControllerPost'];


$this->assertEqual($Controller->PaginatorControllerPost->lastQuery['limit'], 12); $this->assertEqual($Controller->PaginatorControllerPost->lastQueries[1]['limit'], 12);
$this->assertEqual($paging['options']['limit'], 12); $this->assertEqual($paging['options']['limit'], 12);


$Controller = new PaginatorTestController($this->request); $Controller = new PaginatorTestController($this->request);
Expand Down Expand Up @@ -454,7 +453,7 @@ public function testPaginateSpecialType() {


$this->assertEqual(Set::extract($result, '{n}.PaginatorControllerPost.id'), array(2, 3)); $this->assertEqual(Set::extract($result, '{n}.PaginatorControllerPost.id'), array(2, 3));
$this->assertEqual( $this->assertEqual(
$Controller->PaginatorControllerPost->lastQuery['conditions'], $Controller->PaginatorControllerPost->lastQueries[1]['conditions'],
array('PaginatorControllerPost.id > ' => '1') array('PaginatorControllerPost.id > ' => '1')
); );
$this->assertFalse(isset($Controller->params['paging']['PaginatorControllerPost']['options'][0])); $this->assertFalse(isset($Controller->params['paging']['PaginatorControllerPost']['options'][0]));
Expand Down

0 comments on commit b8c00d5

Please sign in to comment.