How to optimize the pagination query? This is completely optimization question, I have a pagination query like this:

$this-&gt;paginate    = array(
        'fields'    =&gt;  array(
                        'DISTINCT Contact.contact_id',
                        'Contact.first_name',
                        'Contact.last_name',
                        'Contact.email',
                        'Contact.created',
                        'ContactGroup.name',
                      ),  
        'conditions' =&gt; array(
                        $this-&gt;conditions,
                        'ContactsContactGroup.contact_group_id'=&gt;$viewList,                        
                        isset($keywordQuery)?$keywordQuery:"",
                      ),      
        'limit'      =&gt; 5,
         'group'     =&gt; array('Contact.contact_id')
                      );
$data = $this-&gt;paginate('ContactsContactGroup');
$data = $this-&gt;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.
Perhaps you could wrap it all in a function:

function pagination($this,$viewList,$keywordQuery)
{
    $this-&gt;paginate    = array(
        'fields'    =&gt;  array(
                        'DISTINCT Contact.contact_id',
                        'Contact.first_name',
                        'Contact.last_name',
                        'Contact.email',
                        'Contact.created',
                        'ContactGroup.name',
                      ),  
        'conditions' =&gt; array(
                        $this-&gt;conditions,
                        'ContactsContactGroup.contact_group_id'=&gt;$viewList,                        
                        isset($keywordQuery)?$keywordQuery:"",
                      ),      
        'limit'      =&gt; 5,
         'group'     =&gt; array('Contact.contact_id')
                      );

   return $this-&gt;paginate('ContactsContactGroup');

}

if(something())
{
    //something
    $data=pagination($this,$viewList,$keywordQuery);
}
else
{
    //other
    $data=pagination($that,$viewList,$keywordQuery);
}


etc..