Doctrine2大集合

Opt*_*mus 5 zend-framework doctrine-orm

在过去的几天里,我一直在玩doctrine2 + ZF设置.

我仍然无法弄清楚的一件事是大型集合集合.例如,假设我们有一个名为Post的实体,每个帖子都可以有很多注释.

<?php
/**
 * @Entity
*/
class Post
{
  /**
   * @OneToMany(targetEntity="Comment", mappedBy="post")
   */
   protected $comments;
}
?>
Run Code Online (Sandbox Code Playgroud)

现在这将加载所有评论,如果我这样做

$post->comments
Run Code Online (Sandbox Code Playgroud)

但是,如果有这样的话,对这个特定的帖子说10000条评论呢?然后所有将被加载,这是不好的.据我所知,切片/分页在学说2.1之前不可用.

有人可以建议我如何分页评论吗?有DQL可能吗?如果是DQL,你在哪里实现这个?我在Post实体中创建一个getComments方法并在那里进行DQL吗?

谢谢比尔

amr*_*ree 10

我正在使用来自https://github.com/beberlei/DoctrineExtensions的分页,它很有用,至少对我而言.

编辑:不确定这会对你有所帮助,但这就是我的分页方式

调节器

// 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;
Run Code Online (Sandbox Code Playgroud)

视图

<?
echo $this->paginationControl($this->records,
                              'Sliding',
                              'partials/pagination.phtml');
?>
Run Code Online (Sandbox Code Playgroud)

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)); ?>">
    &lt; Previous
  </a></li>
<?php else: ?>
    <li class="previous-off">&lt; 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 &gt;
        </a>
    </li>
<?php else: ?>
  <li class="next-off">Next &gt;</li>
<?php endif; ?>
</ul>
<?php endif; ?>
Run Code Online (Sandbox Code Playgroud)