0

This is my query:

class services extends Zend_Db_Table{

 function Get_Services($user_id){
    $DB = Zend_Db_Table_Abstract::getDefaultAdapter();

    $select = $DB->select()
                ->from(array('p' => 'phone_service'))
                ->join(array('u' => 'user_preferences'), 'u.phone_service_id = p.phone_service_id')
                ->where('u.user_preferences_name = ?', 'is_user_package_active')
                ->where('p.user_id = ?', $user_id);
           return $select;
    }
  }

This is my controller:

class ServicesController extends Zend_Controller_Action{
       $instance = new services();
       $select = $instance->Get_Services($user_id);

       $adapter   = new Zend_Paginator_Adapter_DbSelect($select);
       $paginator = new Zend_Paginator($adapter);

       $page=$this->_getParam('page',1);
       $paginator->setItemCountPerPage(10);
       $paginator->setCurrentPageNumber($page);
       $paginator->setPagerange(5);
 }

What I am doing is that first I query all rows and then do this:

 $adapter   = new Zend_Paginator_Adapter_DbSelect($select);
 $paginator = new Zend_Paginator($adapter);

My question is, how can I optimize it so that it queries only 10 rows from the result and not all of them?

Andriy M
  • 76,112
  • 17
  • 94
  • 154
Edward Maya
  • 429
  • 2
  • 10
  • 25

1 Answers1

1

With DbSelect adapter it does query only 10 rows. Profile your queries and inspect - you should see 2 selects AFAIR - one manipulated by ZF to obtain total count of rows and one to get the actual data with LIMIT and OFFSET.

bububaba
  • 2,840
  • 6
  • 25
  • 29
  • but before that i am making query and than applying Zend_Paginator_Adapter_DbSelect does it matter ?? – Edward Maya Apr 04 '12 at 10:56
  • 1
    I don't see you making a query. You are creating a `Select` object, but it's only an object and no queries are made until you make them yourself via `fetchXxx()` methods or the paginator. – bububaba Apr 04 '12 at 10:58