php - cakephp pagination with condition -


i defined condition variable values. need write formate how can write this.

$this->paginator->settings = array('conditions' => array(     if(!empty($this->request->data['filter']['delivery']))     {     'gig.delivery <=' => $this->request->data['filter']['delivery'],     }     if(!empty($this->request->data['filter']['delivering']))     {     "gig.bangsalsodelivering in ({$csv_deliveringfilters})",     }     if(!empty($this->request->data['filter']['servicetype']))     {     "gig.bangsservicetype in ({$csv_bangsservicetypesfilters})",     }     if(!empty($this->request->data['filter']['style']))     {     "gig.bangsstyles in ({$csv_stylefilters})",     }     if(!empty($this->request->data['filter']['fileformate']))     {     "gig.bangsfileformates in ({$csv_fileformatefilters})",     } )); $agetgigsitem = $this->paginator->paginate('gig'); 

conditions array

conditions handled array, not string. example this:

$this->paginator->settings = [     'conditions' => ['gig.field' => 'is value'] ];  $agetgigsitem = $this->paginator->paginate('gig'); 

would apply sql condition:

where gig.field = "is value" 

it a bad idea build conditions using string concatenation. in addition being more cumbersome means you'll need handle escaping of user input yourself.

working solution

given code in question asked:

$conditions = []; $filter = $this->request->data['filter'];  if(!empty($filter['delivery'])) {     $conditions['gig.delivery <='] = $filter['delivery']; }  if(!empty($filter['delivering'])) {     $conditions['gig.bangsalsodelivering'] = explode(',', $filter['delivering']); }  if(!empty($filter['servicetype'])) {     $conditions['gig.bangsservicetype'] = explode(',', $filter['servicetype']); }  if(!empty($filter['style'])) {     $conditions['gig.bangsstyles'] = explode(',', $filter['style']); }  if(!empty($filter['fileformate'])) {     $conditions['gig.bangsfileformates'] = explode(',', $filter['fileformate']); }  $this->paginator->settings['conditions'] = $conditions; $agetgigsitem = $this->paginator->paginate('gig'); 

note array values automatically converted in () condition clauses, , comparison if it's not equals - in array key.

be sure check the documentation version using, know more how use pagination , how query database.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -