在过去的几天里,我一直在玩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 https://github.com/beberlei/DoctrineExtensions,效果很好,至少对我来说。
Edit:不确定这会对您有帮助,但这就是我的分页方法
控制器
// 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;
View
<?
echo $this->paginationControl($this->records,
'Sliding',
'partials/pagination.phtml');
?>
分页.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; ?>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)