This is completely optimization question, I have a pagination query like this:
$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.
-
\$\begingroup\$ you can half the cost of that code by calling paginate only once =) \$\endgroup\$AD7six– AD7six2014年05月01日 17:11:41 +00:00Commented May 1, 2014 at 17:11
1 Answer 1
Perhaps you could wrap it all in a function:
function pagination($this,$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')
);
return $this->paginate('ContactsContactGroup');
}
if(something())
{
//something
$data=pagination($this,$viewList,$keywordQuery);
}
else
{
//other
$data=pagination($that,$viewList,$keywordQuery);
}
etc..
-
\$\begingroup\$ I'm sorry, but really, what sort of code review comes up with an answer that includes
global
? \$\endgroup\$Letharion– Letharion2013年03月14日 19:51:30 +00:00Commented Mar 14, 2013 at 19:51 -
\$\begingroup\$ just a quick way to clean up his duplicate code. either that, or they can just delete it all, start over. \$\endgroup\$phouse– phouse2013年03月14日 20:34:17 +00:00Commented Mar 14, 2013 at 20:34
-
\$\begingroup\$ I get you though. @usii, maybe make the function return $data; \$\endgroup\$phouse– phouse2013年03月14日 20:42:20 +00:00Commented Mar 14, 2013 at 20:42
Explore related questions
See similar questions with these tags.