2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ you can half the cost of that code by calling paginate only once =) \$\endgroup\$ – AD7six May 1 '14 at 17:11
2
\$\begingroup\$

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..

\$\endgroup\$
  • \$\begingroup\$ I'm sorry, but really, what sort of code review comes up with an answer that includes global? \$\endgroup\$ – Letharion Mar 14 '13 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 Mar 14 '13 at 20:34
  • \$\begingroup\$ I get you though. @usii, maybe make the function return $data; \$\endgroup\$ – phouse Mar 14 '13 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.