mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-02-12 06:56:24 +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;
|
$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;
|
||||||
|
|
||||||
|
@ -185,6 +172,17 @@ class PaginatorComponent extends Component {
|
||||||
$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),
|
||||||
|
|
|
@ -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
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -278,7 +277,7 @@ class PaginatorTest extends CakeTestCase {
|
||||||
'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();
|
||||||
|
@ -316,21 +315,21 @@ class PaginatorTest extends CakeTestCase {
|
||||||
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');
|
||||||
|
@ -356,7 +355,7 @@ class PaginatorTest extends CakeTestCase {
|
||||||
$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(
|
||||||
|
@ -369,7 +368,7 @@ class PaginatorTest extends CakeTestCase {
|
||||||
$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(
|
||||||
|
@ -378,14 +377,14 @@ class PaginatorTest extends CakeTestCase {
|
||||||
);
|
);
|
||||||
$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);
|
||||||
|
@ -454,7 +453,7 @@ class PaginatorTest extends CakeTestCase {
|
||||||
|
|
||||||
$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]));
|
||||||
|
|
Loading…
Add table
Reference in a new issue