过去几天我一直在玩doctrine2 + ZF 设置。
我仍然无法弄清楚的一件事是大型数组集合关联。例如,假设我们有一个名为 Post 的实体,每个帖子可以有很多评论。
<?php
/**
* @Entity
*/
class Post
{
/**
* @OneToMany(targetEntity="Comment", mappedBy="post")
*/
protected $comments;
}
?>
现在,如果我这样做,这将加载所有评论
$post->comments
但是,如果有,比如说这个特定帖子有 10000 条评论呢?然后所有将被加载这是不好的。据我所知,在学说 2.1 之前,切片/分页将不可用。
有人可以建议我如何对评论进行分页吗?用 DQL 也许?如果是 DQL,你在哪里实现它?我是否在 Post 实体中创建一个 getComments 方法并在那里执行 DQL?
谢谢
账单
最佳答案
我正在使用来自 https://github.com/beberlei/DoctrineExtensions 的分页,效果很好,至少对我来说。
编辑 :不确定这会帮助你,但这是我如何做我的分页
Controller
// Create the query
$qb = $this->_em->createQueryBuilder();
$qb->select('p')
->from('Identiti_Entities_Pengguna', 'p');
// Sorting
$qb->addOrderBy('p.' . $input->sort, $input->dir);
$q = $qb->getQuery();
// Pagination
$itemPerPage = 100;
$records = new Zend_Paginator(
new DoctrineExtensions\Paginate\PaginationAdapter($q));
$records->setCurrentPageNumber($input->page)
->setItemCountPerPage($itemPerPage)
->setPageRange(10);
$this->view->records = $records;
查看
<?
echo $this->paginationControl($this->records,
'Sliding',
'partials/pagination.phtml');
?>
pagination.html
<?php if ($this->pageCount): ?>
<ul id="pagination-digg">
<li class="previous"><a href="#">Pages: <?=$this->pageCount?></a></li>
<!-- Previous page link -->
<?php if (isset($this->previous)): ?>
<li class="previous"><a href="<?php echo $this->url(array('page' => $this->previous)); ?>">
< Previous
</a></li>
<?php else: ?>
<li class="previous-off">< Previous</li>
<?php endif; ?>
<!-- Numbered page links -->
<?php foreach ($this->pagesInRange as $page): ?>
<?php if ($page != $this->current): ?>
<li>
<a href="<?php echo $this->url(array('page' => $page)); ?>">
<?php echo $page; ?>
</a>
</li>
<?php else: ?>
<li class="active"><?php echo $page; ?></li>
<?php endif; ?>
<?php endforeach; ?>
<!-- Next page link -->
<?php if (isset($this->next)): ?>
<li class="next">
<a href="<?php echo $this->url(array('page' => $this->next)); ?>">
Next >
</a>
</li>
<?php else: ?>
<li class="next-off">Next ></li>
<?php endif; ?>
</ul>
<?php endif; ?>
关于zend-framework - Doctrine2 大集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4833644/