/** * Handles automatic pagination of model records. * * @overwrite to support defaults like limit, querystring settings * @param Model|string $object Model to paginate (e.g: model instance, or 'Model', or 'Model.InnerModel') * @param string|array $scope Conditions to use while paginating * @param array $whitelist List of allowed options for paging * @return array Model query results */ public function paginate($object = null, $scope = array(), $whitelist = array()) { if ($defaultSettings = (array) Configure::read('Paginator')) { $this->paginate += $defaultSettings; } return parent::paginate($object, $scope, $whitelist); }
/** * Renders index action of scaffolded model. * * @param array $params Parameters for scaffolding * @return mixed A rendered view listing rows from Models database table */ protected function _scaffoldIndex($params) { if ($this->controller->beforeScaffold('index')) { $this->ScaffoldModel->recursive = 0; $this->controller->set(Inflector::variable($this->controller->name), $this->controller->paginate()); $this->controller->render($this->request['action'], $this->layout); } elseif ($this->controller->scaffoldError('index') === false) { return $this->_scaffoldError(); } }
/** * List users with filter capability as defined in User::$filterArgs * * This will be useful for ajax autocompletion */ public function lookup(Controller $controller) { $request = $controller->request; $controller->Prg->commonProcess(); $controller->User->Behaviors->attach('Users.UserApiResultFormatter'); $controller->paginate = array('fields' => array('id', 'username', 'name', 'website', 'image', 'bio', 'timezone', 'status', 'created', 'updated'), 'contain' => array('Role' => array('fields' => array('id', 'title', 'alias'))), 'conditions' => $controller->User->parseCriteria($request->query)); $users = $controller->paginate(); $controller->set('_rootNode', 'users'); $controller->set('user', $users); $controller->set('_serialize', 'user'); }
/** * List nodes with filter capability as defined in Node::$filterArgs * * This will be useful for ajax autocompletion */ public function lookup(Controller $controller) { $request = $controller->request; $controller->Prg->commonProcess(); $Node = $controller->{$controller->modelClass}; $Node->Behaviors->attach('Nodes.NodeApiResultFormatter'); $controller->paginate = array('fields' => array('id', 'parent_id', 'type', 'user_id', 'title', 'slug', 'body', 'excerpt', 'status', 'promote', 'path', 'terms', 'created', 'updated', 'publish_start', 'publish_end'), 'contain' => array('User', 'Meta', 'Taxonomy'), 'conditions' => $Node->parseCriteria($request->query)); $nodes = $controller->paginate(); $controller->set('_rootNode', 'nodes'); $controller->set('node', $nodes); $controller->set('_serialize', 'node'); }
public function testPagination() { $this->markTestSkipped('Needs revision'); $objController = new Controller(new CakeRequest('/'), new CakeResponse()); $objController->layout = 'ajax'; $objController->uses = array('User'); $objController->constructClasses(); $objController->request->url = '/'; $objController->paginate = array('fields' => array('username'), 'contain' => false, 'link' => array('Profile' => array('fields' => array('biography'))), 'limit' => 2); $arrayResult = $objController->paginate('User'); $this->assertEquals($objController->params['paging']['User']['count'], 4, 'Paging: total records count: %s'); // Pagination with order on a row from table joined with Linkable $objController->paginate = array('fields' => array('id'), 'contain' => false, 'link' => array('Profile' => array('fields' => array('user_id'))), 'limit' => 2, 'order' => 'Profile.user_id DESC'); $arrayResult = $objController->paginate('User'); $arrayExpected = array(0 => array('User' => array('id' => 4), 'Profile' => array('user_id' => 4)), 1 => array('User' => array('id' => 3), 'Profile' => array('user_id' => 3))); $this->assertEquals($arrayResult, $arrayExpected, 'Paging with order on join table row: %s'); // Pagination without specifying any fields $objController->paginate = array('contain' => false, 'link' => array('Profile'), 'limit' => 2, 'order' => 'Profile.user_id DESC'); $arrayResult = $objController->paginate('User'); $this->assertEquals($objController->params['paging']['User']['count'], 4, 'Paging without any field lists: total records count: %s'); }
/** * Generic index action * * Triggers the following callbacks * - Crud.init * - Crud.beforePaginate * - Crud.afterPaginate * - Crud.beforeRender * * @return void */ protected function _indexAction() { $Paginator = $this->_Collection->load('Paginator'); // Copy pagination settings from the controller if (!empty($this->_controller->paginate)) { $Paginator->settings = array_merge($Paginator->settings, $this->_controller->paginate); } if (!empty($Paginator->settings[$this->_modelName]['findType'])) { $findMethod = $Paginator->settings[$this->_modelName]['findType']; } elseif (!empty($Paginator->settings['findType'])) { $findMethod = $Paginator->settings['findType']; } else { $findMethod = $this->_getFindMethod(null, 'all'); } $subject = $this->trigger('beforePaginate', compact('findMethod')); // Copy pagination settings from the controller if (!empty($this->_controller->paginate)) { $Paginator->settings = array_merge($Paginator->settings, $this->_controller->paginate); } // If pagination settings is using ModelAlias modify that if (!empty($Paginator->settings[$this->_modelName])) { $Paginator->settings[$this->_modelName][0] = $subject->findMethod; $Paginator->settings[$this->_modelName]['findType'] = $subject->findMethod; } else { // Or just work directly on the root key $Paginator->settings[0] = $subject->findMethod; $Paginator->settings['findType'] = $subject->findMethod; } // Push the paginator settings back to Controller $this->_controller->paginate = $Paginator->settings; // Do the pagination $items = $this->_controller->paginate($this->_model); $subject = $this->trigger('afterPaginate', compact('items')); $items = $subject->items; // Make sure to cast any iterators to array if ($items instanceof Iterator) { $items = iterator_to_array($items); } $this->_controller->set(compact('items')); $this->trigger('beforeRender'); }
/** * Over write of core paginate method * to handle auto filtering. * * @param string * @param array * @param array */ public function paginate($object = null, $scope = array(), $whitelist = array()) { $this->request->params = array_merge($this->request->params, Router::parse(urldecode($this->request->here))); $this->_handlePaginatorSorting(); $this->_handlePaginatorFiltering($object); $this->Paginator->settings = $this->paginate; return parent::paginate($object, $scope, $whitelist); }
/** * testPaginateMaxLimit * * @return void */ public function testPaginateMaxLimit() { $Controller = new Controller($this->request); $Controller->uses = array('PaginatorControllerPost', 'ControllerComment'); $Controller->passedArgs[] = '1'; $Controller->constructClasses(); $Controller->request->params['named'] = array('contain' => array('ControllerComment'), 'limit' => '1000'); $result = $Controller->paginate('PaginatorControllerPost'); $this->assertEquals($Controller->params['paging']['PaginatorControllerPost']['options']['limit'], 100); $Controller->request->params['named'] = array('contain' => array('ControllerComment'), 'limit' => '1000', 'maxLimit' => 1000); $result = $Controller->paginate('PaginatorControllerPost'); $this->assertEquals($Controller->params['paging']['PaginatorControllerPost']['options']['limit'], 100); $Controller->request->params['named'] = array('contain' => array('ControllerComment'), 'limit' => '10'); $result = $Controller->paginate('PaginatorControllerPost'); $this->assertEquals($Controller->params['paging']['PaginatorControllerPost']['options']['limit'], 10); $Controller->request->params['named'] = array('contain' => array('ControllerComment'), 'limit' => '1000'); $Controller->paginate = array('maxLimit' => 2000, 'paramType' => 'named'); $result = $Controller->paginate('PaginatorControllerPost'); $this->assertEquals($Controller->params['paging']['PaginatorControllerPost']['options']['limit'], 1000); $Controller->request->params['named'] = array('contain' => array('ControllerComment'), 'limit' => '5000'); $result = $Controller->paginate('PaginatorControllerPost'); $this->assertEquals($Controller->params['paging']['PaginatorControllerPost']['options']['limit'], 2000); }
/** * testDefaultPaginateParams method * * @access public * @return void */ function testDefaultPaginateParams() { $Controller = new Controller(); $Controller->modelClass = 'ControllerPost'; $Controller->params['url'] = array(); $Controller->paginate = array('order' => 'ControllerPost.id DESC'); $Controller->constructClasses(); $results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id'); $this->assertEqual($Controller->params['paging']['ControllerPost']['defaults']['order'], 'ControllerPost.id DESC'); $this->assertEqual($Controller->params['paging']['ControllerPost']['options']['order'], 'ControllerPost.id DESC'); $this->assertEqual($results, array(3, 2, 1)); }
/** * test paginate() and virtualField overlapping with real fields. * * @return void */ public function testPaginateOrderVirtualFieldSharedWithRealField() { $Controller = new Controller($this->request); $Controller->uses = array('PaginatorControllerPost', 'PaginatorControllerComment'); $Controller->constructClasses(); $Controller->PaginatorControllerComment->virtualFields = array('title' => 'PaginatorControllerComment.comment'); $Controller->PaginatorControllerComment->bindModel(array('belongsTo' => array('PaginatorControllerPost' => array('className' => 'PaginatorControllerPost', 'foreignKey' => 'article_id'))), false); $Controller->paginate = array('fields' => array('PaginatorControllerComment.id', 'title', 'PaginatorControllerPost.title')); $Controller->passedArgs = array('sort' => 'PaginatorControllerPost.title', 'dir' => 'asc'); $result = $Controller->paginate('PaginatorControllerComment'); $this->assertEquals(Set::extract($result, '{n}.PaginatorControllerComment.id'), array(1, 2, 3, 4, 5, 6)); }
/** * testPaginate method * * @access public * @return void */ function testPaginate() { $Controller = new Controller(); $Controller->uses = array('Article'); $Controller->passedArgs[] = '1'; $Controller->params['url'] = array(); $Controller->constructClasses(); $Controller->paginate = array('Article' => array('fields' => array('title'), 'contain' => array('User(user)'))); $result = $Controller->paginate('Article'); $expected = array(array('Article' => array('title' => 'First Article'), 'User' => array('user' => 'mariano', 'id' => 1)), array('Article' => array('title' => 'Second Article'), 'User' => array('user' => 'larry', 'id' => 3)), array('Article' => array('title' => 'Third Article'), 'User' => array('user' => 'mariano', 'id' => 1))); $this->assertEqual($result, $expected); $r = $Controller->Article->find('all'); $this->assertTrue(Set::matches('/Article[id=1]', $r)); $this->assertTrue(Set::matches('/User[id=1]', $r)); $this->assertTrue(Set::matches('/Tag[id=1]', $r)); $Controller->paginate = array('Article' => array('contain' => array('Comment(comment)' => 'User(user)'), 'fields' => array('title'))); $result = $Controller->paginate('Article'); $expected = array(array('Article' => array('title' => 'First Article', 'id' => 1), 'Comment' => array(array('comment' => 'First Comment for First Article', 'user_id' => 2, 'article_id' => 1, 'User' => array('user' => 'nate')), array('comment' => 'Second Comment for First Article', 'user_id' => 4, 'article_id' => 1, 'User' => array('user' => 'garrett')), array('comment' => 'Third Comment for First Article', 'user_id' => 1, 'article_id' => 1, 'User' => array('user' => 'mariano')), array('comment' => 'Fourth Comment for First Article', 'user_id' => 1, 'article_id' => 1, 'User' => array('user' => 'mariano')))), array('Article' => array('title' => 'Second Article', 'id' => 2), 'Comment' => array(array('comment' => 'First Comment for Second Article', 'user_id' => 1, 'article_id' => 2, 'User' => array('user' => 'mariano')), array('comment' => 'Second Comment for Second Article', 'user_id' => 2, 'article_id' => 2, 'User' => array('user' => 'nate')))), array('Article' => array('title' => 'Third Article', 'id' => 3), 'Comment' => array())); $this->assertEqual($result, $expected); $r = $Controller->Article->find('all'); $this->assertTrue(Set::matches('/Article[id=1]', $r)); $this->assertTrue(Set::matches('/User[id=1]', $r)); $this->assertTrue(Set::matches('/Tag[id=1]', $r)); $Controller->Article->unbindModel(array('hasMany' => array('Comment'), 'belongsTo' => array('User'), 'hasAndBelongsToMany' => array('Tag')), false); $Controller->Article->bindModel(array('hasMany' => array('Comment'), 'belongsTo' => array('User')), false); $Controller->paginate = array('Article' => array('contain' => array('Comment(comment)', 'User(user)'), 'fields' => array('title'))); $r = $Controller->paginate('Article'); $this->assertTrue(Set::matches('/Article[id=1]', $r)); $this->assertTrue(Set::matches('/User[id=1]', $r)); $this->assertTrue(Set::matches('/Comment[article_id=1]', $r)); $this->assertFalse(Set::matches('/Comment[id=1]', $r)); $r = $this->Article->find('all'); $this->assertTrue(Set::matches('/Article[id=1]', $r)); $this->assertTrue(Set::matches('/User[id=1]', $r)); $this->assertTrue(Set::matches('/Comment[article_id=1]', $r)); $this->assertTrue(Set::matches('/Comment[id=1]', $r)); }
/** * test that using Controller::paginate() falls back to PaginatorComponent * * @return void */ function testPaginateBackwardsCompatibility() { $request = new CakeRequest('controller_posts/index'); $request->params['pass'] = $request->params['named'] = array(); $Controller = new Controller($request); $Controller->uses = array('ControllerPost', 'ControllerComment'); $Controller->passedArgs[] = '1'; $Controller->params['url'] = array(); $Controller->constructClasses(); $expected = array('page' => 1, 'limit' => 20, 'maxLimit' => 100, 'paramType' => 'named'); $this->assertEqual($Controller->paginate, $expected); $results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id'); $this->assertEqual($results, array(1, 2, 3)); $Controller->passedArgs = array(); $Controller->paginate = array('limit' => '-1'); $this->assertEqual($Controller->paginate, array('limit' => '-1')); $Controller->paginate('ControllerPost'); $this->assertIdentical($Controller->params['paging']['ControllerPost']['page'], 1); $this->assertIdentical($Controller->params['paging']['ControllerPost']['pageCount'], 3); $this->assertIdentical($Controller->params['paging']['ControllerPost']['prevPage'], false); $this->assertIdentical($Controller->params['paging']['ControllerPost']['nextPage'], true); }
public function testPagination() { $objController = new Controller(new CakeRequest(), new CakeResponse()); $objController->layout = 'ajax'; $objController->uses = ['LinkableUser']; $objController->constructClasses(); $objController->request->url = '/'; $objController->paginate = ['fields' => ['username'], 'contain' => false, 'link' => ['LinkableProfile' => ['fields' => ['biography']]], 'limit' => 2]; $arrayResult = $objController->paginate('LinkableUser'); $this->assertEquals(4, $objController->params['paging']['LinkableUser']['count'], 'Paging: total records count: %s'); // Pagination with order on a row from table joined with Linkable $objController->paginate = ['fields' => ['id'], 'contain' => false, 'link' => ['LinkableProfile' => ['fields' => ['user_id']]], 'limit' => 2, 'order' => 'LinkableProfile.user_id DESC']; $arrayResult = $objController->paginate('LinkableUser'); $arrayExpected = [0 => ['LinkableUser' => ['id' => 4], 'LinkableProfile' => ['user_id' => 4]], 1 => ['LinkableUser' => ['id' => 3], 'LinkableProfile' => ['user_id' => 3]]]; $this->assertEquals($arrayExpected, $arrayResult, 'Paging with order on join table row: %s'); // Pagination without specifying any fields $objController->paginate = ['contain' => false, 'link' => ['LinkableProfile'], 'limit' => 2, 'order' => 'LinkableProfile.user_id DESC']; $arrayResult = $objController->paginate('LinkableUser'); $this->assertEquals(4, $objController->params['paging']['LinkableUser']['count'], 'Paging without any field lists: total records count: %s'); }
/** * test that using Controller::paginate() falls back to PaginatorComponent * * @return void */ public function testPaginateBackwardsCompatibility() { $request = new CakeRequest('controller_posts/index'); $request->params['pass'] = $request->params['named'] = array(); $response = $this->getMock('CakeResponse', array('httpCodes')); $Controller = new Controller($request, $response); $Controller->uses = array('ControllerPost', 'ControllerComment'); $Controller->passedArgs[] = '1'; $Controller->params['url'] = array(); $Controller->constructClasses(); $expected = array('page' => 1, 'limit' => 20, 'maxLimit' => 100, 'paramType' => 'named'); $this->assertEquals($expected, $Controller->paginate); $results = Hash::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id'); $this->assertEquals(array(1, 2, 3), $results); $Controller->passedArgs = array(); $Controller->paginate = array('limit' => '-1'); $this->assertEquals(array('limit' => '-1'), $Controller->paginate); $Controller->paginate('ControllerPost'); $this->assertSame($Controller->params['paging']['ControllerPost']['page'], 1); $this->assertSame($Controller->params['paging']['ControllerPost']['pageCount'], 3); $this->assertSame($Controller->params['paging']['ControllerPost']['prevPage'], false); $this->assertSame($Controller->params['paging']['ControllerPost']['nextPage'], true); }
public function testPaginate() { $Controller = new Controller(); $Controller->uses = array('TestAddress'); $Controller->params['url'] = array(); $Controller->constructClasses(); $Controller->paginate = array('TestAddress' => array('near', 'fields' => array('address'), 'limit' => 2, 'address' => '1209 La Brad Lane, Tampa, FL')); $result = $Controller->paginate('TestAddress'); $this->assertTrue(!empty($result)); if (!empty($result)) { $result = Set::combine($result, '/' . $this->Address->alias . '/address', '/' . $this->Address->alias . '/distance'); foreach ($result as $key => $distance) { $result[$key] = round($distance, 3); } $expected = array('14348 N Rome Ave, Tampa, 33613 FL' => 0.257, '1180 Magdalene Hill, Florida, US' => 0.499); $this->assertEqual($result, $expected); } $Controller->paginate = array('TestAddress' => array('near', 'fields' => array('address'), 'limit' => 2, 'address' => '1209 La Brad Lane, Tampa, FL', 'page' => 2)); $result = $Controller->paginate('TestAddress'); $this->assertTrue(!empty($result)); if (!empty($result)) { $result = Set::combine($result, '/' . $this->Address->alias . '/address', '/' . $this->Address->alias . '/distance'); foreach ($result as $key => $distance) { $result[$key] = round($distance, 3); } $expected = array('9106 El Portal Dr, Tampa, FL' => 5.331); $this->assertEqual($result, $expected); } $Controller->paginate = array('TestAddress' => array('near', 'fields' => array('address'), 'limit' => 2, 'address' => '1209 La Brad Lane, Tampa, FL', 'unit' => 'm')); $result = $Controller->paginate('TestAddress'); $this->assertTrue(!empty($result)); if (!empty($result)) { $result = Set::combine($result, '/' . $this->Address->alias . '/address', '/' . $this->Address->alias . '/distance'); foreach ($result as $key => $distance) { $result[$key] = round($distance, 3); } $expected = array('14348 N Rome Ave, Tampa, 33613 FL' => 0.16, '1180 Magdalene Hill, Florida, US' => 0.31); $this->assertEqual($result, $expected); } $Controller->paginate = array('TestAddress' => array('near', 'fields' => array('address'), 'limit' => 2, 'address' => '1209 La Brad Lane, Tampa, FL', 'unit' => 'm', 'distance' => 0.25)); $result = $Controller->paginate('TestAddress'); $this->assertTrue(!empty($result)); if (!empty($result)) { $result = Set::combine($result, '/' . $this->Address->alias . '/address', '/' . $this->Address->alias . '/distance'); foreach ($result as $key => $distance) { $result[$key] = round($distance, 3); } $expected = array('14348 N Rome Ave, Tampa, 33613 FL' => 0.16); $this->assertEqual($result, $expected); } }
/** * test paginate() and custom find with returning other query on count operation, * to make sure the correct count is returned. * * @return void */ public function testPaginateCustomFindCount() { $Controller = new Controller($this->request); $Controller->uses = array('PaginatorCustomPost'); $Controller->constructClasses(); $data = array('author_id' => 2, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N'); $Controller->PaginatorCustomPost->create($data); $result = $Controller->PaginatorCustomPost->save(); $this->assertTrue(!empty($result)); $Controller->paginate = array('totalsOperation', 'limit' => 2); $result = $Controller->paginate(); $expected = array(array('PaginatorCustomPost' => array('author_id' => '1', 'total_posts' => '2'), 'Author' => array('user' => 'mariano')), array('PaginatorCustomPost' => array('author_id' => '2', 'total_posts' => '1'), 'Author' => array('user' => 'nate'))); $this->assertEquals($expected, $result); $result = $Controller->params['paging']['PaginatorCustomPost']; $this->assertEquals(2, $result['current']); $this->assertEquals(3, $result['count']); $this->assertEquals(2, $result['pageCount']); $this->assertTrue($result['nextPage']); $this->assertFalse($result['prevPage']); }
/** * test paginate() and virtualField interactions * * @return void */ function testPaginateOrderVirtualField() { $Controller = new Controller(); $Controller->uses = array('ControllerPost', 'ControllerComment'); $Controller->params['url'] = array(); $Controller->constructClasses(); $Controller->ControllerPost->virtualFields = array('offset_test' => 'ControllerPost.id + 1'); $Controller->paginate = array('fields' => array('id', 'title', 'offset_test'), 'order' => array('offset_test' => 'DESC')); $result = $Controller->paginate('ControllerPost'); $this->assertEqual(Set::extract($result, '{n}.ControllerPost.offset_test'), array(4, 3, 2)); $Controller->passedArgs = array('sort' => 'offset_test', 'direction' => 'asc'); $result = $Controller->paginate('ControllerPost'); $this->assertEqual(Set::extract($result, '{n}.ControllerPost.offset_test'), array(2, 3, 4)); }