Zend framework Doctrine2大型集合

Zend framework Doctrine2大型集合,zend-framework,doctrine-orm,Zend Framework,Doctrine Orm,在过去的几天里,我一直在使用doctrine2+ZF设置 我仍然搞不清楚的一件事是大型数组集合协会。例如,假设我们有一个名为Post的实体,每个Post可以有许多注释 <?php /** * @Entity */ class Post { /** * @OneToMany(targetEntity="Comment", mappedBy="post") */ protected $comments; } ?> 但是,如果有,比如说10000条评论,这篇文章怎

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

我仍然搞不清楚的一件事是大型数组集合协会。例如,假设我们有一个名为Post的实体,每个Post可以有许多注释

<?php
/**
 * @Entity
*/
class Post
{
  /**
   * @OneToMany(targetEntity="Comment", mappedBy="post")
   */
   protected $comments;
}
?>
但是,如果有,比如说10000条评论,这篇文章怎么办?然后所有的都将被加载,这是不好的。据我所知,切片/分页将在第2.1条之前不可用

有人能告诉我怎样才能把评论分页吗?也许是DQL?如果是DQL,您在哪里实现它?我是否在Post实体中创建getComments方法并在那里执行DQL

谢谢
比尔

< P>您可以考虑执行<代码> ZeNyPangInActuaAdvultId接口< /C> > < /P> 有关更多详细信息,请参见ZF文档:

我使用的是来自的分页,它非常有效,至少对我来说是这样

编辑:不确定这是否对您有帮助,但下面是我如何分页的

控制器

// 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)); ?>">
    &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; ?>

  • 上一次
  • 下一步

条令2.2现在有了Paginator类。有关如何将其与Zend_Framework一起使用的信息,请参见此链接:

我不想这样做。我试图通过以下方式实现DDD/Doctrine2:)@boosis-Zend和DDD不是相互排斥的,您能解释一下您的评论吗?在分页扩展旁边还有一个名为LargeCollections的扩展。那么您将在哪里使用分页代码呢。在$post->getComments()中?我们可以在实体中使用DQL吗?或者在服务中?你能给出一个代码示例吗@或者在同一个问题上,你会在哪里使用LargeCollection扩展?LargeCollections帮了我的忙!我在我的
Post
实体中添加了以下方法。
公共函数getCommentsPagination($limit=10,$offset=0){$lc=new\DoctrineExtensions\LargeCollections\LargeCollections();$result=$lc->getSliceQuery($this->getComments(),$limit,$offset)->getResult();返回$result;}
很抱歉设置了格式。我在这里找不到如何格式化注释。但是在一个应该是无持久层的实体中添加这样的依赖项是否正确?
<?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; ?>