mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 17:16:18 +00:00
Changed the paginator to do a count after the find.
This commit is contained in:
parent
6fb3c72d49
commit
b8c00d5f56
2 changed files with 35 additions and 38 deletions
|
@ -153,21 +153,8 @@ class PaginatorComponent extends Component {
|
|||
$extra['type'] = $type;
|
||||
}
|
||||
|
||||
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));
|
||||
|
||||
if ($page === 'last' || $page >= $pageCount) {
|
||||
$options['page'] = $page = $pageCount;
|
||||
} elseif (intval($page) < 1) {
|
||||
$options['page'] = $page = 1;
|
||||
if (intval($page) < 1) {
|
||||
$page = 1;
|
||||
}
|
||||
$page = $options['page'] = (int)$page;
|
||||
|
||||
|
@ -185,6 +172,17 @@ class PaginatorComponent extends Component {
|
|||
$defaults = $this->getDefaults($object->alias);
|
||||
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(
|
||||
'page' => $page,
|
||||
'current' => count($results),
|
||||
|
|
|
@ -87,12 +87,11 @@ class PaginatorControllerPost extends CakeTestModel {
|
|||
public $invalidFields = array('name' => 'error_msg');
|
||||
|
||||
/**
|
||||
* lastQuery property
|
||||
* lastQueries property
|
||||
*
|
||||
* @var mixed null
|
||||
* @access public
|
||||
* @var array
|
||||
*/
|
||||
public $lastQuery = null;
|
||||
public $lastQueries = array();
|
||||
|
||||
/**
|
||||
* beforeFind method
|
||||
|
@ -102,7 +101,7 @@ class PaginatorControllerPost extends CakeTestModel {
|
|||
* @return void
|
||||
*/
|
||||
public function beforeFind($query) {
|
||||
$this->lastQuery = $query;
|
||||
array_unshift($this->lastQueries, $query);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -278,7 +277,7 @@ class PaginatorTest extends CakeTestCase {
|
|||
'sort' => 'PaginatorControllerPost.author_id', 'direction' => 'allYourBase'
|
||||
);
|
||||
$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));
|
||||
|
||||
$Controller->request->params['named'] = array();
|
||||
|
@ -315,23 +314,23 @@ class PaginatorTest extends CakeTestCase {
|
|||
*/
|
||||
public function testPageParamCasting() {
|
||||
$this->Controller->Post->expects($this->at(0))
|
||||
->method('hasMethod')
|
||||
->with('paginateCount')
|
||||
->will($this->returnValue(false));
|
||||
|
||||
$this->Controller->Post->expects($this->at(1))
|
||||
->method('find')
|
||||
->will($this->returnValue(2));
|
||||
|
||||
$this->Controller->Post->expects($this->at(2))
|
||||
->method('hasMethod')
|
||||
->with('paginate')
|
||||
->will($this->returnValue(false));
|
||||
|
||||
$this->Controller->Post->expects($this->at(3))
|
||||
|
||||
$this->Controller->Post->expects($this->at(1))
|
||||
->method('find')
|
||||
->will($this->returnValue(array('stuff')));
|
||||
|
||||
|
||||
$this->Controller->Post->expects($this->at(2))
|
||||
->method('hasMethod')
|
||||
->with('paginateCount')
|
||||
->will($this->returnValue(false));
|
||||
|
||||
$this->Controller->Post->expects($this->at(3))
|
||||
->method('find')
|
||||
->will($this->returnValue(2));
|
||||
|
||||
$this->request->params['named'] = array('page' => '1 " onclick="alert(\'xss\');">');
|
||||
$this->Paginator->settings = array('limit' => 1, 'maxLimit' => 10, 'paramType' => 'named');
|
||||
$this->Paginator->paginate('Post');
|
||||
|
@ -356,7 +355,7 @@ class PaginatorTest extends CakeTestCase {
|
|||
$result = $Controller->Paginator->paginate('PaginatorControllerPost');
|
||||
$this->assertEqual($Controller->params['paging']['PaginatorControllerPost']['page'], 1);
|
||||
$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->Paginator->settings = array(
|
||||
|
@ -369,7 +368,7 @@ class PaginatorTest extends CakeTestCase {
|
|||
$result = $Controller->Paginator->paginate('PaginatorControllerPost');
|
||||
$this->assertEqual($Controller->params['paging']['PaginatorControllerPost']['page'], 1);
|
||||
$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(
|
||||
'PaginatorControllerPost' => array(
|
||||
|
@ -378,14 +377,14 @@ class PaginatorTest extends CakeTestCase {
|
|||
);
|
||||
$result = $Controller->Paginator->paginate('PaginatorControllerPost');
|
||||
$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->Paginator->settings = array('limit' => 30, 'maxLimit' => 100, 'paramType' => 'named');
|
||||
$result = $Controller->Paginator->paginate('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);
|
||||
|
||||
$Controller = new PaginatorTestController($this->request);
|
||||
|
@ -454,7 +453,7 @@ class PaginatorTest extends CakeTestCase {
|
|||
|
||||
$this->assertEqual(Set::extract($result, '{n}.PaginatorControllerPost.id'), array(2, 3));
|
||||
$this->assertEqual(
|
||||
$Controller->PaginatorControllerPost->lastQuery['conditions'],
|
||||
$Controller->PaginatorControllerPost->lastQueries[1]['conditions'],
|
||||
array('PaginatorControllerPost.id > ' => '1')
|
||||
);
|
||||
$this->assertFalse(isset($Controller->params['paging']['PaginatorControllerPost']['options'][0]));
|
||||
|
|
Loading…
Add table
Reference in a new issue