How to (re)set CakePhp Paginator to page 1 - cakephp

I have a table of Parts retrieved from a database and a form with a select tag listing PartTypes and a
[Refresh] button. These are used to filter the table to show only parts that match the selected type.
The table is paginated for performance. When I start on Page 1, I can successfully filter my data table
using my select and refresh button, but if I navigate away from page 1 via the Paginator, and try to
refresh the page with a new PartType (rather than all being visible), I get the following error
Error: The requested address '/product/parts/index/page:2' was not found on this server.
I have been through the PaginatorComponent and PaginatorHelper documentation and cannot see how to
reset the page before filtering my data. What am I missing?
/Controller/PartsController.php
public $paginate = array(
'limit' => 12,
'page' => 1,
'order' => array('Part.name' => 'asc' )
);
public function index() {
// ...
// array $qcond holds the query properties for filtering parts
if (count($qcond) > 0)
$this->paginate['conditions'] = $qcond;
// ...
$this->set('parts', $this->paginate());
}
app/View/Parts/index.ctp
<?php
echo $this->Paginator->prev(__('«'), array('tag' => 'li'), null, array('tag' => 'li','class' => 'disabled','disabledTag' => 'a'));
echo $this->Paginator->numbers(array('separator' => '','currentTag' => 'a', 'currentClass' => 'active','tag' => 'li','first' => 1));
echo $this->Paginator->next(__('»'), array('tag' => 'li','currentClass' => 'disabled'), null, array('tag' => 'li','class' => 'disabled','disabledTag' => 'a'));
?>
Problem solved: Issue was not in the Paginator, but in the Form. Had to manually set the form's url to not use /page:2, rather than let CakePhp deduce the url on it's own.

In Cakephp 2.x You can reset page by using bellow code in Controller:
$this->request->params['named']['page']=1;

Related

cakephp How can I convert passed params to named params to prefill element form?

I'm using CakePHP version 2.2.3 I have an element with a search box and a few dropdowns that use CakeDC's search plugin. It works great and just passes the selected/searched items in the URL like this www.mydomain.com/products/cid:1/mid:3/terms:these%20terms where cid is category id, and mid is manufacturer id.
I created pages that allow you to click a category to find all products in that category, but I can't get the category select box, in the element, to select the category of the page it is on. It works if I use the same URL structure as my element submits but I want a clean URL for SEO so I setup the following custom route:
/**
* Categories
*/
Router::connect(
'/products/category/:cid-:slug', // E.g. /products/category/3-my_category
array('controller' => 'products', 'action' => 'category'),
array(
'pass' => array('cid', 'slug'),
'cid' => '[0-9]+'
)
);
this results in a nice looking URL but doesn't pre-select the value of my select list.
I was able to get it working with the code below in my element, but it seems "hacky/clunky"
if(isset($this->params['named']['cid']) && !empty($this->params['named']['cid'])){
echo $this->Form->input('cid', array('label' => false, 'default' => $this->params['named']['cid'], 'options' => $categories, 'empty' => ' ( Category ) '));
}elseif(isset($this->params['pass']['0']) && !empty($this->params['pass']['0'])){
echo $this->Form->input('cid', array('label' => false, 'default' => $this->params['pass']['0'], 'options' => $categories, 'empty' => ' ( Category ) '));
}else{
echo $this->Form->input('cid', array('label' => false, 'options' => $categories, 'empty' => ' ( Category ) '));
}
Also, in my controller I've tried this:
$this->params['named']['cid'] = $this->params['pass']['0'];
but I get this error: Indirect modification of overloaded element of CakeRequest has no effect
I believe the plugin automatically sets the selected value if using named params, unless thats a default behavior of cake. How can I convert the passed params to named params, or can I force my plugin to use passed params?
output from var_dump($this->$params):
object(CakeRequest)[9]
public 'params' =>
array
'plugin' => null
'controller' => string 'products' (length=8)
'action' => string 'category' (length=8)
'named' =>
array
empty
'pass' =>
array
0 => string '2' (length=1)
1 => string 'This_and_that' (length=13)
'cid' => string '2' (length=1)
'slug' => string 'This_and_that' (length=13)
public 'data' =>
array
empty
public 'query' =>
array
empty
Thanks

SEO Friendly URL in CakePHP pagination

I'm using CakePHP v2.42 & would like to have SEO friendly URL in page pagination.
My current pagination is like
http://www.website.com/ubs/page/page:2
What to do to change to
http://www.website.com/ubs/page/2
My Controller is
<?php
class UbsController extends AppController {
public $helpers = array('Html', 'Form');
public function index() {
$this->paginate = array(
'limit' => 100,
);
$ubs = $this->paginate();
$this->set('ubs', $ubs);
}}
My Router is
Router::connect('/ubs', array('controller' => 'ubs', 'action' => 'index'));
Router::connect('/ubs/page/*', array('controller' => 'ubs', 'action' => 'index'));
EDIT - ADD MORE QUESTION
Answer by #kicaj is perfectly correct for the Router & Controller. However, the navigation link only display correctly on the first page.
In the first page navigation link show like this which is correct
http://www.website.com/ubs/
http://www.website.com/ubs/page/2/
http://www.website.com/ubs/page/3/
But navigation link show like this in second/third page page
http://www.website.com/ubs/index/2/
http://www.website.com/ubs/index/2/page:3/
I guess need to edit index.ctp file but not sure what to do.
My current navigation link in index.ctp show like this
$paginator = $this->Paginator;
$paginator->prev("« Prev");
$paginator->numbers(array('modulus' => 200, 'separator' => ' '));
$paginator->next("Next »");
What to change to correct this
Try this:
Router::connect('/ubs/page/:page', array(
'controller' => 'ubs',
'action' => 'index'
), array(
'pass' => array(
'page'
),
'page' => '[\d]+'
));
and in index action in ubs controller add code bellow:
public function index($page = 1) {
// ...
$this->request->params['named']['page'] = $page;
// ...
}
In your Paginator Helper you can choose the right friendly url you want with setting some options
url The URL of the paginating action. ‘url’ has a few sub options as well:
sort The key that the records are sorted by.
direction The direction of the sorting. Defaults to ‘ASC’.
page The page number to display.
There here an example .
$this->Paginator->options(array(
'url' => array(
'sort' => 'email', 'direction' => 'desc', 'page' => 6,
'lang' => 'en'
)
));
the source : Modifying the options PaginatorHelper uses

$this->Model-id not set with CakePHP 1.3

I am trying to retrieve the id of a record in my database in the index() method of my cns_controller.php file. I want to use it for a find(). However, the $this->Cn->id is not returning a value, therefore the find() doesn't work either.
$uses = array('Cn', 'Release');
/*check non-null value in released_user_id,
showing Release tab has been signed off*/
$releaseSignedOff = $this->Release->find('all', array(
'conditions' => array('Release.cn_id =' => $this->Cn->id,
'Release.released_user_id !=' => null)
));
(sizeof($releaseSignedOff) > 0) ? $releaseSignedOff = true : $releaseSignedOff = false;
$this->set('releaseSignedOff', $releaseSignedOff);
Any ideas?
Thanks
I ended up not using my index view, instead I looked up the most recent record in my cns database table and redirected to the view view using the returned value as a parameter:
if (!$id) {
$most_recent_change_note = $this->Cn->find('first', array(
'order' => array('Cn.id DESC')
));
$this->redirect(array('controller' => 'cns',
'action' => 'view/'.$most_recent_change_note['Cn']['id']));
}
For the pagination, I ended up using the $neighbors feature of CakePHP:
function get_neighbors($id) {
$neighbors = $this->Cn->find('neighbors', array('field' => 'id',
'value' => $id));
$this->set('neighbors', $neighbors);
}
Then in my view view, I used those values to create links:
<div class="paging">
<?php echo ($neighbors['next']) ? $html->link('<< Prev',
array('controller'=>'cns',
'action'=>'view/'.$neighbors['next']['Cn']['id'])).' | ' : '<< Prev | ';
echo $html->link('Next >>', array('controller'=>'cns',
'action'=>'view/'.$neighbors['prev']['Cn']['id'])); ?>
</div>
Thanks to Charles and Ross for helping me reach this conclusion. :)

ie wont support dtds in feed cakephp

Ok I'm trying to create a rss feed with cakephp rss helper for some post of an app. I followed the cakephp book to the letter and it wont work with internet explorer... When I open it with Opera it work but with ie it says "Internet Explorer does not support feeds with DTDs."...
I know microsoft is not supporting dtds because a security thread but how can I fix this issue? The company where I work uses ie by standard so changing the browser is not an option...
here is the code... So you can see there is not a mayor modification in it...
default.ctp
echo $this->Rss->header();
if (!isset($documentData)) {
$documentData = array();
}
if (!isset($channelData)) {
$channelData = array();
}
if (!isset($channelData['title'])) {
$channelData['title'] = $title_for_layout;
}
$channel = $this->Rss->channel(array(), $channelData, $content_for_layout);
echo $this->Rss->document($documentData,$channel);
index.ctp
$this->set('documentData', array(
'xmlns:dc' => 'http://purl.org/dc/elements/1.1/'));
$this->set('channelData', array(
'title' => __("Most Recent Hitos", true),
'link' => $this->Html->url('/', true),
'description' => __("Most recent Hitos.", true),
'language' => 'en-us'));
foreach ($posts as $post) {
$postLink = array(
'controller' => 'soportes',
'action' => 'view',
$post['Soporte']['id']);
// You should import Sanitize
App::import('Sanitize');
// This is the part where we clean the body text for output as the description
// of the rss item, this needs to have only text to make sure the feed validates
$bodyText = preg_replace('=\(.*?\)=is', '', $post['Hito']['actividad']);
$bodyText = $this->Text->stripLinks($bodyText);
$bodyText = Sanitize::stripAll($bodyText);
$bodyText = $this->Text->truncate($bodyText, 400, array(
'ending' => '...',
'exact' => true,
'html' => true,
));
echo $this->Rss->item(array(), array(
'title' => $post['Hito']['actividad'],
'link' => $postLink,
'guid' => array('url' => $postLink, 'isPermaLink' => 'true'),
'description' => $bodyText,
'dc:creator' => $post['Hito']['user_id'],
'pubDate' => $post['Hito']['fecha_sugerida']));
}
On a very basic level it might be because of the debug level in core.php - cake outputs the render time I think - you could try setting Configure::write('debug', 0); in your controller action to see if it renders in IE? I've had issues with XML / RSS and IE due to this before.

CakePHP - Paginating an Array

Cake handles pagination of a model with a simple $this->paginate(), but what should I use if I want to paginate a array of values?
The Scenario is like this:
$this->set('sitepages', $this->paginate());
This code in my index() returns an array like
Array
(
[0] => Array
(
[Sitepage] => Array
(
[id] => 13
[name] => Home
[urlslug] => home
[parent_id] => 1
[page_title] => Welcome to KIAMS, Pune
[order] => 1
)
)
[1] => Array
(
[Sitepage] => Array
(
[id] => 26
[name] => About Us
[urlslug] => aboutus
[parent_id] => 1
[page_title] =>
[order] => 2
)
)
[2] => Array
(
[Sitepage] => Array
(
[id] => 27
[name] => Overview of KIAMS
[urlslug] => aboutus/overview
[parent_id] => 26
[page_title] =>
[order] => 2
)
)
I retrieved the same data using $this->Sitepage->find('all') and then performed some manipulations as required and form a array which is very similar to the above one, but the ordering gets changed. I want to paginate this new array and pass it to the view. I tried
$this->set('sitepages',$this->paginate($newarray))
But the data is not getting paginated. Can some one please help with paginating the $newarray in CakePHP?
To paginate in CakePHP you need to pass select conditions to the paginate() call.
Other data manipulation should be done in afterFind(), in your model file.
If you don't need these changes to be done in every single retrieval, you might as well consider creating a new model file pointing to the very same table as the current one, and adding an afterFind() method to that new file.
I've just dealt with this same problem...
I found the only way is to use the paginate() function to handle all the logic, rather than passing it a custom array. However, this isn't as bad as it seems:
$this->paginate = array(
'limit' => 2,
'order' => array(
'Report.name' => 'asc'
),
'conditions' => array(
'Account.id' => $this->foo()
),
);
$reports = $this->paginate();
In this example, I'm paginating Reports - but some Reports will not be included, depending on which Account they belong to (Account has some relationship with Report, hasmany, etc.).
By writing $paginate inside your action, you can use a custom callback for the conditions array. So function foo() can be written in your controller (or shoved to model) and returns an array of valid Account IDs.
I found I could easily rewrite my custom logic in this function - keeping both me and the Cake paginator happy.
Hope that helps!
I'm using cakephp version 1.3 and it seems this one is working:
at controller:
$result = $this->Paginate('Event');
$results = $this->Task->humanizeEvent($result);
$this->set('events', $results);
and it seems to display as a normal paginated array, at the view (setup your pagination in view as normal).
The humanizeEvent function just edits a field on the results array, to make it a sentence based on other fields inside the array, and it seems to work properly.
$this->paginate($newarray) is the wrong way to paginate. The first parameter cannot be an array. It must be a model name. You may want to study pagination setup from the manual. This will order alphabetically:
var $paginate = array(
'limit' => 25,
'order' => array(
'Sitepage.name' => 'asc'
)
);
$this->set('sitepages', $this->paginate('Sitepage'));
I create a component checking the paginator code..
Is not the best thing, but It work for me.....
Controller
$slicedArray = array_slice($fullArray,($page - 1) * $this->PaginatorArray->limit ,$this->PaginatorArray->limit)
$this->params['paging'] = $this->PaginatorArray->getParamsPaging('MyModel', $page, $total,count($slicedArray));
$this->helpers[] = 'Paginator';
Component
<?php
/* SVN FILE: $Id$ */
/**
* Pagination Array Component class file.
* #subpackage cake.cake.libs.view.helpers
*/
class PaginatorArrayComponent {
var $limit = 40;
var $step = 1;
function startup( & $controller){
$this->controller = & $controller;
}
function getParamsPaging($model, $page, $total, $current){
$pageCount = ceil($total / $this->limit);
$prevPage = '';
$nextPage = '';
if($page > 1)
$prevPage = $page - 1;
if($page + 1 <= $pageCount)
$nextPage = $page + 1;
return array(
$model => array(
'page' => $page,
'current' => $current,
'count' => $total,
'prevPage' => $prevPage,
'nextPage' => $nextPage,
'pageCount' => $pageCount,
'defaults' => array(
'limit' => $this->limit,
'step' => $this->step,
'order' => array(),
'conditions' => array(),
),
'options' => array(
'page' => $page,
'limit' => $this->limit,
'order' => array(),
'conditions' => array(),
)
)
);
}
}
?>
There was a bug in find("count") and it returned incorrect count if the query resulted in records for only in group. This has been fixed click here

Resources