学说 2 和 zend paginator

发布于 2024-11-19 00:29:42 字数 405 浏览 4 评论 0原文

我想将学说与 zend_paginator

这里使用一些示例查询:

$allArticleObj = $this->_em->getRepository('文章'); $qb = $this->_em->createQueryBuilder();

 $qb->add('select', 'a')
            ->add('来自', '文章a')
            ->设置第一个结果(0)
            ->setMaxResults(5);

有没有示例代码表明我们可以为原则 2 查询生成器编写 zend_paginator 适配器?

i want to use doctrine with zend_paginator

here some example query :

$allArticleObj =
$this->_em->getRepository('Articles');
$qb = $this->_em->createQueryBuilder();

    $qb->add('select', 'a')
            ->add('from', 'Articles a')
            ->setFirstResult(0)
            ->setMaxResults(5);

is there any example code to show we can write a zend_paginator adapter for doctrine 2 query builder?

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

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

发布评论

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

评论(4

若相惜即相离 2024-11-26 00:29:42

您不需要实现 Zend_Paginator_Adapter_Interface。它已经由 Zend_Paginator_Adapter_Iterator 实现。

您可以简单地将 Doctrines 的 Paginator 传递给 Zend_Paginator_Adapter_Iterator,然后再传递给 Zend_Paginator。然后调用 Zend_Paginator::setItemCountPerPage($perPage)Zend_Paginator::setCurrentPageNumber($current_page)。像这样:

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 in your view, use it just like your currently use     
}
 

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

说明:

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 进行分页的代码。

代码在这里(尽管它可能不适用于最新的 DoctrineORM 2.2),但示例是有效的: https://github.com/kkruecke/zf-beginners-doctrine2/tree/master/ch7

You don't need to implement Zend_Paginator_Adapter_Interface. It is already implement by Zend_Paginator_Adapter_Iterator.

You can simply pass Doctrines' Paginator to Zend_Paginator_Adapter_Iterator, which you pass to Zend_Paginator. Then you call Zend_Paginator::setItemCountPerPage($perPage) and Zend_Paginator::setCurrentPageNumber($current_page). 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 in your view, use it just like your currently use     
}
 

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.

Code is here (although it might not all work with latest DoctrineORM 2.2) but the example is valid: https://github.com/kkruecke/zf-beginners-doctrine2/tree/master/ch7

晨曦÷微暖 2024-11-26 00:29:42

我非常惊讶找到一个不会导致 Doctrine 2 和 ZF1 的大型集合出现性能问题的适配器示例是多么困难。

我的灵魂确实使用 Doctrine\ORM\Tools\Pagination\来自 Doctrine 2.2 的 Paginator 就像 @Kurt 的答案;然而不同之处在于,这将从 getItems 返回学说分页器(它本身就是一个 \Iterator),而不会对整个查询结果进行水合。

use Doctrine\ORM\Tools\Pagination\Paginator as DoctrinePaginator;
use Doctrine\ORM\Query;

class DoctrinePaginatorAdapter implements \Zend_Paginator_Adapter_Interface
{
    protected $query;

    public function __construct(Query $query)
    {
        $this->query = $query;
    }

    public function count()
    {
        return $this->createDoctrinePaginator($this->query)->count();
    }

    public function getItems($offset, $itemCountPerPage)
    {
        $this->query->setFirstResult($offset)
                    ->setMaxResults($itemCountPerPage);

        return $this->createDoctrinePaginator($this->query);
    }

    protected function createDoctrinePaginator(Query $query, $isFetchJoinQuery = true)
    {
        return new DoctrinePaginator($query, $isFetchJoinQuery);
    }

} 

I was very surprised how hard it was to find an adapter example that doesn't cause performance issues with large collections for Doctrine 2 and ZF1.

My soultion does use the the Doctrine\ORM\Tools\Pagination\Paginator from Doctrine 2.2 just like @Kurt's answer; however the difference that this will return the doctrine paginator (Which is it's self an \Iterator) from getItems without the entire query results being hydrated.

use Doctrine\ORM\Tools\Pagination\Paginator as DoctrinePaginator;
use Doctrine\ORM\Query;

class DoctrinePaginatorAdapter implements \Zend_Paginator_Adapter_Interface
{
    protected $query;

    public function __construct(Query $query)
    {
        $this->query = $query;
    }

    public function count()
    {
        return $this->createDoctrinePaginator($this->query)->count();
    }

    public function getItems($offset, $itemCountPerPage)
    {
        $this->query->setFirstResult($offset)
                    ->setMaxResults($itemCountPerPage);

        return $this->createDoctrinePaginator($this->query);
    }

    protected function createDoctrinePaginator(Query $query, $isFetchJoinQuery = true)
    {
        return new DoctrinePaginator($query, $isFetchJoinQuery);
    }

} 
茶底世界 2024-11-26 00:29:42

当然,结果是您必须实现 Zend_Paginator_Adapter_Interface,这本质上意味着实现两个方法:

count()

getItems($offset, $perPage) )

你的适配器将接受 Doctrine 查询作为构造函数参数。

原则上,getItems() 部分实际上很简单。只需将 $offset$perPage 限制添加到查询中(就像您在示例中所做的那样),然后执行查询。

实际上,count() 业务往往比较棘手。我将遵循 Zend_Paginator_Adapter_DbSelect 的示例,将 Zend_Db 操作替换为 Doctrine 类似操作。

The upshot, of course, is that you have to implement the Zend_Paginator_Adapter_Interface, which essentially means implementing the two methods:

count()

getItems($offset, $perPage)

Your adapter would accept the Doctrine query as a constructor argument.

In principle, the getItems() part is actually straightforward. Simply, add the $offset and $perPage restrictions to the query - as you are doing in your sample - and execute the query.

In practice, it's the count() business that tends to be tricky. I'd follow the example of Zend_Paginator_Adapter_DbSelect, replacing the Zend_Db operations with their Doctrine analogues.

梦醒灬来后我 2024-11-26 00:29:42

请更改

use Doctrine\ORM\Tools\Pagination as Paginator;

use Doctrine\ORM\Tools\Pagination\Paginator as Paginator;

Please change

use Doctrine\ORM\Tools\Pagination as Paginator;

to

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