Zend_Paginator / 教义 2

发布于 2024-10-20 02:47:27 字数 218 浏览 8 评论 0 原文

我将 Doctrine 2 与我的 Zend Framework 应用程序一起使用,典型的查询结果可能会产生一百万(或更多)搜索结果。

我想使用 Zend_Paginator 来符合这个结果集。但是,我不想将所有结果作为数组返回并使用数组适配器,因为这效率低下,而是我想向分页器提供当时的总行数和基于限制/偏移量的结果数组。

使用数组适配器可以实现这一点吗?还是我需要创建自己的分页适配器?

I'm using Doctrine 2 with my Zend Framework application and a typical query result could yield a million (or more) search results.

I want to use Zend_Paginator in line with this result set. However, I don't want to return all the results as an array and use the Array adapter as this would be inefficient, instead I would like to supply the paginator the total amount of rows then and array of results based on limit/offset amounts.

Is this doable using the Array adapter or would I need to create my own pagination adapter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤独患者 2024-10-27 02:47:27

您必须扩展自己的适配器。数组适配器以您不希望的方式工作 - 它接收一个数组并根据当前设置返回其中的一部分。您需要的是一个新的适配器,它将采用 DQL 语句并设置限制/偏移量。

You will have to extend your own adapter. The Array adapter works the way you don't want - it receives an array and return a portion of it based on the current settings. What you'll need is a new adapter that will take DQL statemenet and set the limit/offset.

彼岸花似海 2024-10-27 02:47:27

您不需要实现 Zend_Paginator_Adapter_Interface,因为它已经由 Zend_Paginator_Adapter_Iterator 实现了。

相反,可以简单地将 Doctrine 的 Paginator 传递给 Zend_Paginator_Adapter_Iterator,如下所示:

use Doctrine\ORM\Tools\Pagination as Paginator; // goes at top of file

SomeController::someAction() 
{
 $dql = "SELECT s, c FROM Square\Entity\StampItem s JOIN s.country c ".' ORDER BY '.  $orderBy . ' ' . $dir;
 
 $query = $this->getEntityManager()->createQuery($dql);
 $d2_paginator = new Paginator($query); 
 
 $d2_paginator_iter = $d2_paginator->getIterator(); // returns \ArrayIterator object
 
 $adapter =  new \Zend_Paginator_Adapter_Iterator($d2_paginator_iter);
  
 $zend_paginator = new \Zend_Paginator($adapter);          
                       
 $zend_paginator->setItemCountPerPage($perPage)
            ->setCurrentPageNumber($current_page);

 $this->view->paginator = $zend_paginator; 
}
 

然后,您可以像平常一样在视图脚本中使用分页器。

说明:

Zend_Paginator 的构造函数可以采用 Zend_Paginator_Adapter_Interface,它由 Zend_Paginator_Adpater_Iterator 实现。现在,Zend_Paginator_Adapter_Iterator 的构造函数采用 \Iterator 接口。这个 \Iterator 还必须实现 \Countable (正如您可以通过查看 Zend_Paginator_Adapter_Iterator 的构造函数看到的那样)。由于 Paginator::getIterator() 方法返回一个 \ArrayIterator,根据定义它符合要求(因为 \ArrayIterator 实现了 \Iterator\Countable)。

请参阅“Zend Framework:初学者指南”代码中从 Doctrine 1 到 Doctrine 2 的移植,从 Doctrine 1 到 Doctrine: https://github.com/kkruecke/zf-beginners-doctrine2。它包括使用 Zend_Paginator_Adapter_Iterator 和 Doctrine 2' Doctrine\ORM\Tools\Pagination\Paginator 来使用 Zend_Paginator 进行分页的代码。

You don't need to implement Zend_Paginator_Adapter_Interface, as it is already implement by Zend_Paginator_Adapter_Iterator.

Instead can simply pass Doctrine's Paginator to Zend_Paginator_Adapter_Iterator like this:

use Doctrine\ORM\Tools\Pagination as Paginator; // goes at top of file

SomeController::someAction() 
{
 $dql = "SELECT s, c FROM Square\Entity\StampItem s JOIN s.country c ".' ORDER BY '.  $orderBy . ' ' . $dir;
 
 $query = $this->getEntityManager()->createQuery($dql);
 $d2_paginator = new Paginator($query); 
 
 $d2_paginator_iter = $d2_paginator->getIterator(); // returns \ArrayIterator object
 
 $adapter =  new \Zend_Paginator_Adapter_Iterator($d2_paginator_iter);
  
 $zend_paginator = new \Zend_Paginator($adapter);          
                       
 $zend_paginator->setItemCountPerPage($perPage)
            ->setCurrentPageNumber($current_page);

 $this->view->paginator = $zend_paginator; 
}
 

Then you use paginator in the view script just like you ordinarly do.

Explanation:

Zend_Paginator's constructor can take a Zend_Paginator_Adapter_Interface, which Zend_Paginator_Adpater_Iterator implements. Now, Zend_Paginator_Adapter_Iterator's constructor takes an \Iterator interface. This \Iterator must also implement \Countable (as you can see by looking at Zend_Paginator_Adapter_Iterator's constructor). Since Paginator::getIterator() method returns an \ArrayIterator, it by definition it fits the bill (since \ArrayIterator implements both \Iterator and \Countable).

See this port from Doctrine 1 to Docrine 2 of the code for "Zend Framework: A Beginner's Guide" from Doctrine 1 to Doctrine: https://github.com/kkruecke/zf-beginners-doctrine2. It includes code for paginating with Zend_Paginator using Zend_Paginator_Adapter_Iterator with Doctrine 2' Doctrine\ORM\Tools\Pagination\Paginator.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文