如何提高zend paginator分页的性能

时间:2012-02-02 09:44:36

标签: mysql zend-framework zend-db zend-paginator

我已经制作了一个基于AJAX的Zend_Paginator,但是我不想从查询中获取所有记录。例如;我每页有10个项目,所以我希望我的查询只获取10行。

以下是代码: -

$request = $this->getRequest();
$phone_service_id  =  $request->getParam("id");
$registry = Zend_Registry::getInstance();  
$DB = $registry['DB'];
$sql ="SELECT caller_name,call_number,call_start_time,call_duration,call_direction
       FROM CALL_LOG
       WHERE phone_service_id = $phone_service_id";
$result = $DB->fetchAll($sql);
$page=$this->_getParam('page',1);
$paginator = Zend_Paginator::factory($result);
$paginator->setItemCountPerPage(10);
$paginator->setCurrentPageNumber($page);
$this->view->paginator=$paginator;
$page = $paginator->getCurrentPageNumber();
$perPage = $paginator->getItemCountPerPage();
$total = $paginator->getTotalItemCount();
$A = ($page - 1) * $perPage + 1;
$B = min($A + $perPage - 1, $total);
$C = $total;
$this->view->assign('url', $request->getBaseURL());
$this->view->assign('total',$total );
$this->view->assign('page',$page );
$this->view->assign('A',$A );
$this->view->assign('B',$B );
$this->view->assign('C',$C );

如何限制我的查询,以便首先提取第一页的10行,如果是第二页,那么它只提取11到20行,依此类推?

3 个答案:

答案 0 :(得分:2)

将查询形成Zend_Db_Select对象。然后使用Zend_Paginator_Adapter_DbSelect。它会自动为您的查询添加限制和偏移量。

答案 1 :(得分:2)

你可以做这样的事情

$select = $DB->select()
             ->from('CALL_LOG', array('caller_name','call_number','call_start_time','call_duration','call_direction'))
             ->where('phone_service_id = ?', $phone_service_id);
$adapter = new Zend_Paginator_Adapter_DbSelect($select);
$paginator = new Zend_Paginator($adapter);

然后是你的其余代码。

答案 2 :(得分:1)

如果您查看Zend_Paginator manual page,您将看到如果您为Zend_Paginator提供Zend_Select对象,则您已经内置了所需的行为。目前,您正在为其提供array

要更改此设置,您应更改代码,如下所示: -

$request = $this->getRequest();
$phone_service_id  =  $request->getParam("id");

//altered code follows
$select = new Zend_Db_Select(Zend_Db_Table::getDefaultAdapter());
$select = $DB->select()
         ->from('CALL_LOG', array(
                'caller_name',
                'call_number',
                'call_start_time',
                'call_duration',
                'call_direction'))
         ->where('phone_service_id = ?', $phone_service_id);
$paginator = Zend_Paginator::factory($select);
//End of altered code

$paginator->setItemCountPerPage(10);
$page=$this->_getParam('page',1);
$paginator->setCurrentPageNumber($page);
$this->view->paginator=$paginator;
$page = $paginator->getCurrentPageNumber();
$perPage = $paginator->getItemCountPerPage();
$total = $paginator->getTotalItemCount();
$A = ($page - 1) * $perPage + 1;
$B = min($A + $perPage - 1, $total);
$C = $total;
$this->view->assign('url', $request->getBaseURL());
$this->view->assign('total',$total );
$this->view->assign('page',$page );
$this->view->assign('A',$A );
$this->view->assign('B',$B );
$this->view->assign('C',$C );

Zend_Paginator现在将在需要时查询您的数据库 成功使用Zend Framework的先决条件是阅读manualAPI documentation,你真的会苦苦挣扎。

就个人而言,我已经找到了最好的文档作为代码,你应该阅读它当时正在使用的任何组件,你会学到很多。

请注意我无法测试此代码,但我认为没有任何理由说明它不起作用。