如何提高 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 行,依此类推?
I have made an AJAX based Zend_Paginator, however I dont want to fetch all records from the query. For instance; I have ten items per page so I want that my query fetch only 10 rows.
Here is the code:-
$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 );
How can I limit my query so it extract first the 10 rows for the first page and if its second page so it only extract from 11 to 20 rows and so on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以执行类似的操作
,然后执行其余的代码。
You can do something like this
and then rest of your code.
将您的查询形成为 Zend_Db_Select 对象。然后使用 Zend_Paginator_Adapter_DbSelect。它会自动为您的查询添加限制和偏移量。
Form your query to a Zend_Db_Select Object. Then use the Zend_Paginator_Adapter_DbSelect. It will automatically add limit and offset to your query.
如果您查看 Zend_Paginator 手册页,如果您为 Zend_Paginator 提供 Zend_Select 对象,您将看到您需要的行为已经内置。目前,您正在为其提供一个数组。
要更改此设置,您应该按如下方式更改代码:-
Zend_Paginator 现在将在需要时查询数据库。
成功使用 Zend Framework 的先决条件是阅读手册和API 文档,如果没有,你真的会很挣扎。
就我个人而言,我发现最好的文档是代码,无论你当时使用什么组件,你都应该阅读它,你会学到很多东西。
请注意,我无法测试此代码,但我看不出它不起作用的任何原因。
If you take a look at the Zend_Paginator manual page, you will see that the behaviour you require is already built in if you provide Zend_Paginator with a Zend_Select object. At the moment, you are providing it with an array.
To change this you should alter your code as follows:-
Zend_Paginator will now look after querying your database as and when it is required.
A pre-requisite of using Zend Framework successfuly is to have read the manual and the API documentation, you really will struggle without.
Personally, I have found the best documentation to be the code, you should read it for whatever component you are using at the time, you will learn a lot.
Please note I have not been able to test this code, but I don't see any reason why it shouldn't work.