限制初始条令QueryBuilder响应;加载更多”;


Limit initial Doctrine QueryBuilder Response, Allow for "Load More"

我使用Doctrine的查询生成器来获取数据表,并利用Zend在页面上显示信息。我正在使用一些Ajax来启用"加载更多"函数,但我需要限制返回的初始行数,同时仍然可以使用利用相同存储库函数getAllNotifications的Ajax调用来"加载更多的"。

这可以在Zend控制器内的动作方法中实现吗?如果没有,最好的方法是什么?

控制器中的动作方式:

public function historyAction()
{
    $this->view->pageTitle = "Notification Center";
    $this->view->notifications = 'Home'EntityUtils::getRepository('Notification')->getAllNotifications();
}

在控制器中加载更多操作方法:

public function loadMoreNotificationsAction()
{
    $offset = $this->getParam('offset');
    $limit = $this->getParam('limit', 30);
    //get notifications
    $this->view->notifications = 'Home'EntityUtils::getRepository('Notification')->getAllNotifications($offset, $limit);
    //render notifications using partialLoop
    $notificationsHtml = $this->view->partialLoop("notificationHistoryRow.phtml", $this->view->notifications);
    //return notification html via json
    $this->_helper->json($notificationsHtml);
}

存储库中的getAllNotifications方法,上面使用过:

public function getAllNotifications($offset = null, $limit = null)
{
    $qb = $this->_em->createQueryBuilder();
    $qb->select('n.date_posted, nt.name as type, nt.class, n.id, n.title, n.message, n.recipient_params')
        ->from(''Home'Entity'Notification', 'n')
        ->orderBy('n.date_posted', 'DESC')
        ->leftJoin('n.notification_type', 'nt');
    if ($limit) {
        $qb->setMaxResults($limit);
    }
    if ($offset) {
        $qb->setFirstResult($offset);
    }
    $results = $qb->getQuery()->getArrayResult();
    return $results;
}

用于"加载更多"功能的JavaScript:

$(function() {
var offset = 0;
$("#load-more").click(function(e) {
    e.preventDefault();
    $.post("load-more-notifications", {offset: offset, limit: 30}, function(response) {
        $(".notification-table-body").append(response);
        offset += 30;
    });
});
});

通过利用getAllNotifications(null, 30)存储库方法的现有参数,我能够在不干扰加载更多功能的情况下为初始页面加载设置限制。

public function historyAction()
{
    $this->view->pageTitle = "Notification Center";
    $this->view->headScript()->appendFile("/js/tableSorter/jquery.tablesorter.min.js");
    $this->view->notifications = 'Fisdap'EntityUtils::getRepository('Notification')->getAllNotifications(null, 30);
}