Merge branch 'paginate-optimization' into 2.5

Fixes #3333
This commit is contained in:
mark_story 2014-05-12 22:11:27 -04:00
commit 5b143fcfd7
2 changed files with 10 additions and 8 deletions

View file

@ -202,6 +202,8 @@ class PaginatorComponent extends Component {
$count = 0; $count = 0;
} elseif ($object->hasMethod('paginateCount')) { } elseif ($object->hasMethod('paginateCount')) {
$count = $object->paginateCount($conditions, $recursive, $extra); $count = $object->paginateCount($conditions, $recursive, $extra);
} elseif ($page === 1 && count($results) < $limit) {
$count = count($results);
} else { } else {
$parameters = compact('conditions'); $parameters = compact('conditions');
if ($recursive != $object->recursive) { if ($recursive != $object->recursive) {

View file

@ -358,7 +358,7 @@ class PaginatorComponentTest extends CakeTestCase {
$this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']); $this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
$this->assertEquals(array(3, 2, 1), $results); $this->assertEquals(array(3, 2, 1), $results);
$Controller->request->params['named'] = array('sort' => 'NotExisting.field', 'direction' => 'desc'); $Controller->request->params['named'] = array('sort' => 'NotExisting.field', 'direction' => 'desc', 'limit' => 2);
$Controller->Paginator->paginate('PaginatorControllerPost'); $Controller->Paginator->paginate('PaginatorControllerPost');
$this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']); $this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
$this->assertEquals(array(), $Controller->PaginatorControllerPost->lastQueries[1]['order'][0], 'no order should be set.'); $this->assertEquals(array(), $Controller->PaginatorControllerPost->lastQueries[1]['order'][0], 'no order should be set.');
@ -367,7 +367,7 @@ class PaginatorComponentTest extends CakeTestCase {
'sort' => 'PaginatorControllerPost.author_id', 'direction' => 'allYourBase' 'sort' => 'PaginatorControllerPost.author_id', 'direction' => 'allYourBase'
); );
$results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id'); $results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerPost'), '{n}.PaginatorControllerPost.id');
$this->assertEquals(array('PaginatorControllerPost.author_id' => 'asc'), $Controller->PaginatorControllerPost->lastQueries[1]['order'][0]); $this->assertEquals(array('PaginatorControllerPost.author_id' => 'asc'), $Controller->PaginatorControllerPost->lastQueries[0]['order'][0]);
$this->assertEquals(array(1, 3, 2), $results); $this->assertEquals(array(1, 3, 2), $results);
$Controller->request->params['named'] = array(); $Controller->request->params['named'] = array();
@ -466,7 +466,7 @@ class PaginatorComponentTest extends CakeTestCase {
$result = $Controller->Paginator->paginate('PaginatorControllerPost'); $result = $Controller->Paginator->paginate('PaginatorControllerPost');
$this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']); $this->assertEquals(1, $Controller->params['paging']['PaginatorControllerPost']['page']);
$this->assertEquals(array(1, 2, 3), Hash::extract($result, '{n}.PaginatorControllerPost.id')); $this->assertEquals(array(1, 2, 3), Hash::extract($result, '{n}.PaginatorControllerPost.id'));
$this->assertTrue(isset($Controller->PaginatorControllerPost->lastQueries[1]['contain'])); $this->assertTrue(isset($Controller->PaginatorControllerPost->lastQueries[0]['contain']));
$Controller->Paginator->settings = array( $Controller->Paginator->settings = array(
'PaginatorControllerPost' => array( 'PaginatorControllerPost' => array(
@ -475,14 +475,14 @@ class PaginatorComponentTest extends CakeTestCase {
); );
$result = $Controller->Paginator->paginate('PaginatorControllerPost'); $result = $Controller->Paginator->paginate('PaginatorControllerPost');
$this->assertEquals(array(2, 3), Hash::extract($result, '{n}.PaginatorControllerPost.id')); $this->assertEquals(array(2, 3), Hash::extract($result, '{n}.PaginatorControllerPost.id'));
$this->assertEquals(array('PaginatorControllerPost.id > ' => '1'), $Controller->PaginatorControllerPost->lastQueries[1]['conditions']); $this->assertEquals(array('PaginatorControllerPost.id > ' => '1'), $Controller->PaginatorControllerPost->lastQueries[0]['conditions']);
$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->assertEquals(12, $Controller->PaginatorControllerPost->lastQueries[1]['limit']); $this->assertEquals(12, $Controller->PaginatorControllerPost->lastQueries[0]['limit']);
$this->assertEquals(12, $paging['options']['limit']); $this->assertEquals(12, $paging['options']['limit']);
$Controller = new PaginatorTestController($this->request); $Controller = new PaginatorTestController($this->request);
@ -551,7 +551,7 @@ class PaginatorComponentTest extends CakeTestCase {
$this->assertEquals(array(2, 3), Hash::extract($result, '{n}.PaginatorControllerPost.id')); $this->assertEquals(array(2, 3), Hash::extract($result, '{n}.PaginatorControllerPost.id'));
$this->assertEquals( $this->assertEquals(
$Controller->PaginatorControllerPost->lastQueries[1]['conditions'], $Controller->PaginatorControllerPost->lastQueries[0]['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]));