Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cake Php paginator issue

I am facing a weired issue. I have written code for pagination. Everything is working as expected, but only conditions are not working(only certain conditions) Here is my code for pagination.

//declaration 
public $paginate = array(
        'limit' => 50,
        'paramType' => 'querystring'
    ); 
//use in action
$this->paginate['ApiLog'] = array('limit' => 50, 'order' => 'ApiLog.id DESC', 'paramType' => 'querystring');

$this->paginate['ApiLog']['conditions'] = array('ApiLog.log_type' => 2);
$this->paginate['ApiLog']['joins'] = array(               
            array(
                'table' => 'users',
                'alias' => 'User',
                'type' => 'LEFT',
                'conditions' => array('User.id = ApiLog.user_id')
            )                
        );
$this->Paginator->settings = $this->paginate['ApiLog'];
$apilogs = $this->Paginator->paginate('ApiLog');

This code is working prefect on development environment and return logs that has type 2, but in production it return only logs that has type 1. I have spent whole day to figure out issue. If i do add any other condition in conditions array that does take effect but not log_type. I print Query logs, in where clause it always show log_type = '1' I have cleared cache as well. Any help appreciated, thanks.

like image 347
Karan Avatar asked Aug 18 '16 11:08

Karan


People also ask

How can I paginate in CakePHP?

CakePHP eases the burden on the developer by providing a quick, easy way to paginate data. Pagination in CakePHP is offered by a component in the controller. You then use View\Helper\PaginatorHelper in your view templates to generate pagination controls.

What is pagination used for?

Pagination is the process of separating print or digital content into discrete pages. For print documents and some online content, pagination also refers to the automated process of adding consecutive numbers to identify the sequential order of pages.


2 Answers

If your code working fine in local machine but not working in production then you have to clear all files in tmp/cache/model and tmp/cahe/persistance. make sure you have given writable permission to tmp folder.

It means you have to update Cake Model schema when you add new field, remove existing field or make any changes in database schema. In production mode If schema cache file not found then it will create new cache files based on current schema on database. In development mode on every execution it will update schema cache.

Still its issue in pagination then you have to follow CakePHP 2.x documentation. Here I assume your code is in ApiLogsController and method is index. as per documentation your code should be.

public function index() {
    $this->Paginator->settings = array(
        'limit' => 50,
        'order' => 'ApiLog.id DESC'
        'paramType' => 'querystring',
        'conditions'=>  array('ApiLog.log_type' => 2),
        'recursive'=>-1, // should be used with joins
        'joins'=>array(
            array(
                'table'=>'users',
                'type'=>'LEFT',
                'alias'=>'User',
                'conditions'=>array('User.id = ApiLog.user_id')
            )
        )
    );
    $data = $this->Paginator->paginate('ApiLog');
    $this->set(compact('data'));
}

OR

// your default setting in ApiLogController class
public $components = array('Paginator');
public $paginate = array(
    'limit' => 50,
    'paramType' => 'querystring'
);

public function index() {
// overriding/extending default settings

$this->Paginator->settings['order']='ApiLog.id DESC';
$this->Paginator->settings['conditions']=array('ApiLog.log_type' => 2),
$this->Paginator->settings['recursive']= -1;
$this->Paginator->settings['joins']=array(
        array(
            'table'=>'users',
            'type'=>'LEFT',
            'alias'=>'User',
            'conditions'=>array('User.id = ApiLog.user_id')
        )
    );

$data = $this->Paginator->paginate('ApiLog');
$this->set(compact('data'));
}
like image 52
Haresh Vidja Avatar answered Sep 24 '22 22:09

Haresh Vidja


I have fixed issue, This issue was due to data type declared into database. In production it was tinyint(1) and in development it given int(1). It is saving correct value in database in both environment but in condition tinyint(1) is working with only 0 & 1 not with 2.

I didn't understand reason behind this.

like image 29
Karan Avatar answered Sep 25 '22 22:09

Karan