Big collection of Doctrine2

I have been playing with doctrine2 + ZF installation for the past few days.

One of the things that I still cannot understand is the collection of large arrays. For example, suppose we have an object called Mail, and each message can have many comments.

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

Now this will load all comments if I do

$post->comments

But what if there are, say, 10,000 comments for this particular post? Then everything will be loaded, which is not very good. And as far as I know, fragment / pagination is not available until there is doctrine 2.1.

Can someone advise me how I can write comments? Perhaps with DQL? if DQL, where do you implement this? Do I create a getComments method in a Post object and there DQL?

Thanks bill

+5
3

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;

<?
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; ?>
+10

Zend_Paginator_Adapter_Interface.

. ZF:

+1

Doctrine 2.2 now has the Paginator class. See this link for use with Zend_Framework: Answer to a question about how to use D2 Paginator with Zend_Paginator

0
source

All Articles