0

我制作了一个基于 AJAX 的 Zend_Paginator,但是我不想从查询中获取所有记录。例如; 我每页有十个项目,所以我希望我的查询只获取 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 行,依此类推?

4

3 回答 3

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);

然后是其余的代码。

于 2012-02-02T12:19:13.000 回答
2

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

于 2012-02-02T10:13:15.113 回答
1

如果您查看Zend_Paginator 手册页,您将看到如果您为 Zend_Paginator 提供 Zend_Select 对象,您需要的行为已经内置。目前,您正在为它提供一个数组

要改变这一点,你应该改变你的代码如下: -

$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 的先决条件是阅读手册API 文档,如果没有,你真的会很吃力。

就个人而言,我发现最好的文档是代码,你应该阅读它以了解你当时使用的任何组件,你会学到很多东西。

请注意,我无法测试此代码,但我看不出它不应该工作的任何原因。

于 2012-02-02T13:09:13.697 回答