Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cakephp paginator helper shows error when delete the last record from the last page

Tags:

cakephp-2.3

I just want to redirect that to the last index if the last record is deleted from the last page. please help me to do this.

<?php                       
    echo $this->Paginator->prev
      ($this->Html->image('prev.png'), array('escape' => false), 
    array(), null, array('class' => 'prev'));
    echo $this->Paginator->counter
      ('Page {:page} of {:pages}, Total Records {:count}');                     
    echo $this->Paginator->next($this->Html->image
      ('next.png'), array('escape' => false), 
        array(), null, array('class' => 'next'));
 ?>
like image 831
Sunny Mahajan Avatar asked Oct 21 '22 15:10

Sunny Mahajan


1 Answers

As of CakePHP 2.3 - Out of Range page requests will throw an exception.

However the documentation is not correct in saying that the paging parameters will be available to you in $this->request->params['paging'], because those are defined after the exception is thrown. (This problem has been fixed in CakePHP 2.4.x two months ago)

So, to get the effect you want you can do something like this in your controller:

public function index() {
    try {
        $paginatedData = $this->Paginator->paginate();
    } catch (NotFoundException $e) {
        //get current page
        $page = $this->request->params['named']['page'];
        if( $page > 1 ){
            //redirect to previous page
            $this->redirect( array( "page" => $page-1 ) );
        }else{
            $paginatedData = array(); //no data to paginate so use empty array()
                                      //you will have to check for this in the view and no longer display the pagination links, since they will NOT be defined
        }
    }
}
like image 102
Ilie Pandia Avatar answered Oct 23 '22 21:10

Ilie Pandia