Skip to content
Permalink
Browse files

refs #backport-paginate-multiple-queries Backporting cakephp 3.3 feat…

…ure to paginate multiple queries
  • Loading branch information...
chav170 committed Nov 3, 2016
1 parent 25b62b6 commit 20da4484deb8e6bc228428d78e44b2f89ea72c75
@@ -80,14 +80,30 @@ class PaginatorComponent extends Component {
* - `paramType` What type of parameters you want pagination to use?
* - `named` Use named parameters / routed parameters.
* - `querystring` Use query string parameters.
* - `queryScope` By using request parameter scopes you can paginate multiple queries in the same controller action.
*
* ```
* $paginator->paginate = array(
* 'Article' => array('queryScope' => 'articles'),
* 'Tag' => array('queryScope' => 'tags'),
* );
* ```
*
* Each of the above queries will use different query string parameter sets
* for pagination data. An example URL paginating both results would be:
*
* ```
* /dashboard/articles[page]:1/tags[page]:2
* ```
*
* @var array
*/
public $settings = array(
'page' => 1,
'limit' => 20,
'maxLimit' => 100,
'paramType' => 'named'
'paramType' => 'named',
'queryScope' => null
);
/**
@@ -225,7 +241,8 @@ public function paginate($object = null, $scope = array(), $whitelist = array())
'order' => $order,
'limit' => $limit,
'options' => Hash::diff($options, $defaults),
'paramType' => $options['paramType']
'paramType' => $options['paramType'],
'queryScope' => $options['queryScope'],
);
if (!isset($this->Controller->request['paging'])) {
@@ -317,6 +334,9 @@ public function mergeOptions($alias) {
$request = $this->Controller->request->query;
break;
}
if ($defaults['queryScope']) {
$request = Hash::get($request, $defaults['queryScope'], array());
}
$request = array_intersect_key($request, array_flip($this->whitelist));
return array_merge($defaults, $request);
}
@@ -337,7 +357,8 @@ public function getDefaults($alias) {
'page' => 1,
'limit' => 20,
'maxLimit' => 100,
'paramType' => 'named'
'paramType' => 'named',
'queryScope' => null
);
return $defaults;
}
@@ -509,7 +509,8 @@ public function testPaginateExtraParams() {
'contain' => array('ControllerPaginateModel'),
'group' => 'Comment.author_id',
'maxLimit' => 10,
'paramType' => 'named'
'paramType' => 'named',
'queryScope' => null
);
$this->assertEquals($expected, $Controller->ControllerPaginateModel->extra);
$this->assertEquals($expected, $Controller->ControllerPaginateModel->extraCount);
@@ -519,7 +520,8 @@ public function testPaginateExtraParams() {
'foo', 'contain' => array('ControllerPaginateModel'),
'group' => 'Comment.author_id',
'maxLimit' => 10,
'paramType' => 'named'
'paramType' => 'named',
'queryScope' => null
)
);
$Controller->Paginator->paginate('ControllerPaginateModel');
@@ -528,7 +530,8 @@ public function testPaginateExtraParams() {
'group' => 'Comment.author_id',
'type' => 'foo',
'maxLimit' => 10,
'paramType' => 'named'
'paramType' => 'named',
'queryScope' => null
);
$this->assertEquals($expected, $Controller->ControllerPaginateModel->extra);
$this->assertEquals($expected, $Controller->ControllerPaginateModel->extraCount);
@@ -706,6 +709,7 @@ public function testMergeOptionsModelSpecific() {
'limit' => 20,
'maxLimit' => 100,
'paramType' => 'named',
'queryScope' => null,
'Post' => array(
'page' => 1,
'limit' => 10,
@@ -717,7 +721,7 @@ public function testMergeOptionsModelSpecific() {
$this->assertEquals($this->Paginator->settings, $result);
$result = $this->Paginator->mergeOptions('Post');
$expected = array('page' => 1, 'limit' => 10, 'paramType' => 'named', 'maxLimit' => 50);
$expected = array('page' => 1, 'limit' => 10, 'paramType' => 'named', 'maxLimit' => 50, 'queryScope' => null);
$this->assertEquals($expected, $result);
}
@@ -738,7 +742,75 @@ public function testMergeOptionsNamedParams() {
'paramType' => 'named',
);
$result = $this->Paginator->mergeOptions('Post');
$expected = array('page' => 10, 'limit' => 10, 'maxLimit' => 100, 'paramType' => 'named');
$expected = array('page' => 10, 'limit' => 10, 'maxLimit' => 100, 'paramType' => 'named', 'queryScope' => null);
$this->assertEquals($expected, $result);
}
/**
* test mergeOptions with custom scope
*
* @return void
*/
public function testMergeOptionsCustomScope() {
$this->request->params['named'] = array(
'page' => 10,
'limit' => 10,
'scope' => array(
'page' => 2,
'limit' => 5,
)
);
$this->Paginator->settings = array(
'page' => 1,
'limit' => 20,
'maxLimit' => 100,
'findType' => 'myCustomFind',
);
$result = $this->Paginator->mergeOptions('Post');
$expected = array(
'page' => 10,
'limit' => 10,
'maxLimit' => 100,
'findType' => 'myCustomFind',
'paramType' => 'named',
'queryScope' => null
);
$this->assertEquals($expected, $result);
$this->Paginator->settings = array(
'page' => 1,
'limit' => 20,
'maxLimit' => 100,
'findType' => 'myCustomFind',
'queryScope' => 'non-existent',
);
$result = $this->Paginator->mergeOptions('Post');
$expected = array(
'page' => 1,
'limit' => 20,
'maxLimit' => 100,
'findType' => 'myCustomFind',
'paramType' => 'named',
'queryScope' => 'non-existent',
);
$this->assertEquals($expected, $result);
$this->Paginator->settings = array(
'page' => 1,
'limit' => 20,
'maxLimit' => 100,
'findType' => 'myCustomFind',
'queryScope' => 'scope',
);
$result = $this->Paginator->mergeOptions('Post');
$expected = array(
'page' => 2,
'limit' => 5,
'maxLimit' => 100,
'findType' => 'myCustomFind',
'paramType' => 'named',
'queryScope' => 'scope',
);
$this->assertEquals($expected, $result);
}
@@ -760,7 +832,14 @@ public function testMergeOptionsCustomFindKey() {
'findType' => 'myCustomFind'
);
$result = $this->Paginator->mergeOptions('Post');
$expected = array('page' => 10, 'limit' => 10, 'maxLimit' => 100, 'paramType' => 'named', 'findType' => 'myCustomFind');
$expected = array(
'page' => 10,
'limit' => 10,
'maxLimit' => 100,
'paramType' => 'named',
'findType' => 'myCustomFind',
'queryScope' => null
);
$this->assertEquals($expected, $result);
}
@@ -785,7 +864,13 @@ public function testMergeOptionsQueryString() {
'paramType' => 'querystring',
);
$result = $this->Paginator->mergeOptions('Post');
$expected = array('page' => 99, 'limit' => 75, 'maxLimit' => 100, 'paramType' => 'querystring');
$expected = array(
'page' => 99,
'limit' => 75,
'maxLimit' => 100,
'paramType' => 'querystring',
'queryScope' => null
);
$this->assertEquals($expected, $result);
}
@@ -810,7 +895,7 @@ public function testMergeOptionsDefaultWhiteList() {
'paramType' => 'named',
);
$result = $this->Paginator->mergeOptions('Post');
$expected = array('page' => 10, 'limit' => 10, 'maxLimit' => 100, 'paramType' => 'named');
$expected = array('page' => 10, 'limit' => 10, 'maxLimit' => 100, 'paramType' => 'named', 'queryScope' => null);
$this->assertEquals($expected, $result);
}
@@ -837,7 +922,12 @@ public function testMergeOptionsExtraWhitelist() {
$this->Paginator->whitelist[] = 'fields';
$result = $this->Paginator->mergeOptions('Post');
$expected = array(
'page' => 10, 'limit' => 10, 'maxLimit' => 100, 'paramType' => 'named', 'fields' => array('bad.stuff')
'page' => 10,
'limit' => 10,
'maxLimit' => 100,
'paramType' => 'named',
'queryScope' => null,
'fields' => array('bad.stuff')
);
$this->assertEquals($expected, $result);
}
@@ -853,15 +943,15 @@ public function testMergeOptionsMaxLimit() {
'paramType' => 'named',
);
$result = $this->Paginator->mergeOptions('Post');
$expected = array('page' => 1, 'limit' => 200, 'maxLimit' => 100, 'paramType' => 'named');
$expected = array('page' => 1, 'limit' => 200, 'maxLimit' => 100, 'paramType' => 'named', 'queryScope' => null);
$this->assertEquals($expected, $result);
$this->Paginator->settings = array(
'maxLimit' => 10,
'paramType' => 'named',
);
$result = $this->Paginator->mergeOptions('Post');
$expected = array('page' => 1, 'limit' => 20, 'maxLimit' => 10, 'paramType' => 'named');
$expected = array('page' => 1, 'limit' => 20, 'maxLimit' => 10, 'paramType' => 'named', 'queryScope' => null);
$this->assertEquals($expected, $result);
$this->request->params['named'] = array(
@@ -872,7 +962,7 @@ public function testMergeOptionsMaxLimit() {
'paramType' => 'named',
);
$result = $this->Paginator->mergeOptions('Post');
$expected = array('page' => 1, 'limit' => 500, 'maxLimit' => 100, 'paramType' => 'named');
$expected = array('page' => 1, 'limit' => 500, 'maxLimit' => 100, 'paramType' => 'named', 'queryScope' => null);
$this->assertEquals($expected, $result);
}
@@ -1332,10 +1332,15 @@ public function testPaginateBackwardsCompatibility() {
$Controller->uses = array('ControllerPost', 'ControllerComment');
$Controller->passedArgs[] = '1';
$Controller->params['url'] = array();
$Controller->params['named'] = array(
'posts' => array(
'page' => 2,
'limit' => 2,
),
);
$Controller->constructClasses();
$expected = array('page' => 1, 'limit' => 20, 'maxLimit' => 100, 'paramType' => 'named');
$expected = array('page' => 1, 'limit' => 20, 'maxLimit' => 100, 'paramType' => 'named', 'queryScope' => null);
$this->assertEquals($expected, $Controller->paginate);
$results = Hash::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id');
$this->assertEquals(array(1, 2, 3), $results);
@@ -1347,6 +1352,13 @@ public function testPaginateBackwardsCompatibility() {
$this->assertSame($Controller->params['paging']['ControllerPost']['pageCount'], 3);
$this->assertFalse($Controller->params['paging']['ControllerPost']['prevPage']);
$this->assertTrue($Controller->params['paging']['ControllerPost']['nextPage']);
$this->assertNull($Controller->params['paging']['ControllerPost']['queryScope']);
$Controller->paginate = array('queryScope' => 'posts');
$Controller->paginate('ControllerPost');
$this->assertSame($Controller->params['paging']['ControllerPost']['page'], 2);
$this->assertSame($Controller->params['paging']['ControllerPost']['pageCount'], 2);
$this->assertSame($Controller->params['paging']['ControllerPost']['queryScope'], 'posts');
}
/**
Oops, something went wrong.

0 comments on commit 20da448

Please sign in to comment.
You can’t perform that action at this time.