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\$
1
  • \$\begingroup\$ you can half the cost of that code by calling paginate only once =) \$\endgroup\$ Commented May 1, 2014 at 17:11

1 Answer 1

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.