Pagination in a rich domain model

I use a rich domain model in my application. Key ideas were made there . For example, I have User and Comment objects. They are defined as follows:

<?php
class Model_User extends Model_Abstract {

    public function getComments() {
        /**
        * @var Model_Mapper_Db_Comment
        */
        $mapper = $this->getMapper();
        $commentsBlob = $mapper->getUserComments($this->getId());
        return new Model_Collection_Comments($commentsBlob);
    }

}

class Model_Mapper_Db_Comment extends Model_Mapper_Db_Abstract {

    const TABLE_NAME = 'comments';

    protected $_mapperTableName = self::TABLE_NAME;

    public function getUserComments($user_id) {
        $commentsBlob = $this->_getTable()->fetchAllByUserId((int)$user_id);
        return $commentsBlob->toArray();
    }
}

class Model_Comment extends Model_Abstract {

}
?>

The Mapper function getUserComments simply returns something like:

return $this->getTable->fetchAllByUserId($user_id)

which is an array. fetchAllByUserId accepts the parameters $ count and $ offset, but I don’t know whether to pass them from my controller to this function through the model without overwriting all the model code.

So the question is how can I organize pagination using model data (getComments). Is there a "nice" method for getting comments from 5 to 10, and not for everyone, since getComments is returned by default.

+3
4

:

class Model_Mapper_Db_Comment extends Model_Mapper_Db_Abstract {

    public function getUserCommentsPaginator($user_id) {
        $select = $this->_getTable()->select()->where('user_id = ?', (int)$user_id);
        $paginator = Zend_Paginator::factory($select, 'DbSelect');
        return $paginator;
    }
} 

class Model_User extends Model_Abstract implements Zend_Auth_Adapter_Interface {

    public function getCommentsPaginator() {
        $paginator = $this->getMapper(null, 'Comment')->getUserCommentsPaginator($this->id);
        $paginator->setFilter(new App_Filter_Array_Collection('Model_Collection_Comments'));
        return $paginator;
    }
}

Zend_Paginate user_id, Model. Zend_Paginate, Model ( Model_Collection, ). (, ..).

, .

0

Zend_Paginator , . array() Iterator ( a Zend_Db_Table_Rowset)

$paginator = Zend_Paginator::factory($model->getComments());
$paginator->setItemCountPerPage(5);
$paginator->setCurrentPageNumber($this->getRequest()->getParam('page',1));
$this->view->comments = $paginator;

:

<?php foreach($this->comments as $comment): ?>
   Render your HTML for the comment
<?php endforeach; ?>
<?php echo $this->paginationControl($this->comments, 'Sliding', '_pagination.phtml'); ?>

() paginationControl() ( ):

<?php if ($this->pageCount): ?>
<div class="paginationControl">
  <?php if (isset($this->previous)): ?>
    <a href="<?= $this->url(array(’page’ => $this->previous)); ?>">&lt; Previous</a> |
  <?php else: ?>
    <span class="disabled">&lt; Previous</span> |
  <?php endif; ?>

  <?php foreach ($this->pagesInRange as $page): ?>
    <?php if ($page != $this->current): ?>
      <a href="<?= $this->url(array(’page’ => $page)); ?>"><?= $page; ?></a> |
    <?php else: ?>
      <?= $page; ?> |
    <?php endif; ?>
  <?php endforeach; ?>

  <?php if (isset($this->next)): ?>
    <a href="<?= $this->url(array(’page’ => $this->next)); ?>">Next &gt;</a>
  <?php else: ?>
    <span class="disabled">Next &gt;</span>
  <?php endif; ?>
</div>
<?php endif; ?>

Zend_Paginator Google.

+2

, , , , , .

, - Model_Collection_Abstract, , , , .

, , :

<?PHP
    //$comments is a subclass of Model_Collection_Abstract, which implements the paging stuff
    $comments = $user->getComments(); 
    $comments->setStart(10);
    $comments->setPageLength(10);

    $numPages = $comments->numPages(); //can be derived from the pagelength and the collection internal record store.

   $currentPage = $comments->currentPage(); //can be derived from start and page length

    foreach($comments as $comment){
       //this code runs up to ten times, starting at the tenth element in the collection.
    }

, , . .

, N ( N = ), , , / , .

: Zend_Paginator . implements Iterator, , Zend_Paginator . (), !

+2

. ? 1) - Zend_Paginator -. ( - ..), itemCountPerPage, currentPageNumbe, ( ),

$comments = $this->getCommentsMapper()->getPage($itemCountPerPage, $currentPage);

( , ),

$totalComments = $this->getCommentsMapper()->getTotal();

, , " ". "Zendy", .

2) Zend_Paginator, LIE . , , , , "total count" . .

3) , "", . , . :

` $ commentsService = $this- > getCommentsService(); $ CommentsService- > setItemCountPerPage (10);// , 10 config.ini

$commentsPage = $this- > getCommentsService() → getPage (12); $ this- > view- > commentsPage = $commentsPage;

`

- ` $ CommentsPage- > GetItems();//

$commentsPage- > getCurrentPageNumber();

$commentsPage- > getTotalItemsCount();

`

.

.

Now I am choosing between the first and third approaches, perhaps I will go with the third.

+2
source

Source: https://habr.com/ru/post/1746589/


All Articles