Reputation: 366
This is completely optimization question, I have a pagination query like that
$this->paginate = array(
'fields' => array(
'DISTINCT Contact.contact_id',
'Contact.first_name',
'Contact.last_name',
'Contact.email',
'Contact.created',
'ContactGroup.name',
),
'conditions' => array(
$this->conditions,
'ContactsContactGroup.contact_group_id'=>$viewList,
isset($keywordQuery)?$keywordQuery:"",
),
'limit' => 5,
'group' => array('Contact.contact_id')
);
$data = $this->paginate('ContactsContactGroup');
$data = $this->paginate('ContactsContactGroup');
this query is called in every if and else statement, I have four conditions of if
and else
, and in all conditions the above piece of code is written.
I want to optimize, and avoid the big line of code in every condition, how can I optimize it, any answer will be appreciated.
Upvotes: 1
Views: 370
Reputation: 14173
You can use the another method in your class that will return the array instead of posting it everywhere. Add the following code
private function getPaginate($viewList, $keywordQuery) {
$this->paginate = array( 'fields' => array( 'DISTINCT Contact.contact_id', 'Contact.first_name', 'Contact.last_name', 'Contact.email', 'Contact.created', 'ContactGroup.name', ), 'conditions' => array( $this->conditions, 'ContactsContactGroup.contact_group_id'=>$viewList, isset($keywordQuery)?$keywordQuery:"", ), 'limit' => 5, 'group' => array('Contact.contact_id') );
//optional: return the result
return $this->paginate;
}
and then you can use
$this->getPaginate( $viewlist, $keywordQuery );
$data = $this->paginate('ContactsContactGroup');
$data = $this->paginate('ContactsContactGroup');
Upvotes: 1
Reputation: 16086
What i use as regular practice on search page i keep adding condition in if and else and concatenate it at the end to the main query. In your condition i think we can do like below:
//here is your if and else statement
$condition = array();
if(someconditions) {
$condition['user'] = 'suresh'; //your conditions
else if(some conditions)
$condition['email'] ='[email protected]';
//And than add it to main query
$this->paginate = array(
'fields' => array(
'DISTINCT Contact.contact_id',
'Contact.first_name',
'Contact.last_name',
'Contact.email',
'Contact.created',
'ContactGroup.name',
),
'conditions' => array(
$condition
'ContactsContactGroup.contact_group_id'=>$viewList,
isset($keywordQuery)?$keywordQuery:"",
),
'limit' => 5,
'group' => array('Contact.contact_id')
);
$data = $this->paginate('ContactsContactGroup');
$data = $this->paginate('ContactsContactGroup');
Upvotes: 1