問題描述
我想在 zend_paginator 中使用學(xué)說
i want to use doctrine with zend_paginator
這里有一些示例查詢:
$allArticleObj =$this->_em->getRepository('文章');$qb = $this->_em->createQueryBuilder();
$allArticleObj = $this->_em->getRepository('Articles'); $qb = $this->_em->createQueryBuilder();
$qb->add('select', 'a')
->add('from', 'Articles a')
->setFirstResult(0)
->setMaxResults(5);
是否有任何示例代碼表明我們可以為 Dotct 2 查詢構(gòu)建器編寫 zend_paginator 適配器?
is there any example code to show we can write a zend_paginator adapter for doctrine 2 query builder?
推薦答案
結(jié)果當(dāng)然是你必須實(shí)現(xiàn)Zend_Paginator_Adapter_Interface
,這實(shí)質(zhì)上意味著實(shí)現(xiàn)兩個方法:
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)
您的適配器將接受 Doctrine 查詢作為構(gòu)造函數(shù)參數(shù).
Your adapter would accept the Doctrine query as a constructor argument.
原則上,getItems()
部分實(shí)際上很簡單.只需將 $offset
和 $perPage
限制添加到查詢中 - 正如您在示例中所做的 - 并執(zhí)行查詢.
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.
在實(shí)踐中,count()
業(yè)務(wù)往往很棘手.我會遵循 Zend_Paginator_Adapter_DbSelect
的例子,用它們的 Doctrine
類似物替換 Zend_Db
操作.
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.
這篇關(guān)于學(xué)說 2 和 Zend 分頁器的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!