我已将Sonata NewsBundle扩展为包含新闻区域。这是为了向英国游客展示英国新闻,美国游客美国新闻等。
尽管在Pager对象中使用的查询是正确的,但nbResults计数和页面正在计算所有区域的新闻 - 而不仅仅是查询中的区域。这导致页面数量不正确。
pager.getResults()正在获取正确的帖子。这是Pager对象的twig转储以供参考。这是英国的新闻,在档案中仅显示2个帖子,但在3个页面中有26个:
Pager {#1493 ▼
#queryBuilder: null
#page: 1
#maxPerPage: 10
#lastPage: 3.0
#nbResults: "26"
#cursor: 1
#parameters: []
#currentMaxLink: 1
#maxRecordLimit: false
#maxPageLinks: 0
#results: null
#resultsCounter: 0
#query: ProxyQuery {#1504 ▼
#queryBuilder: QueryBuilder {#1492 ▼
-_em: EntityManager {#537 …11}
-_dqlParts: array:9 [▶]
-_type: 0
-_state: 1
-_dql: "SELECT p, t FROM Acme\NewsBundle\Entity\Post p LEFT JOIN p.tags t WITH t.enabled = true LEFT JOIN p.author a WITH a.enabled = true LEFT JOIN p.regions r WHERE p.enabled = :enabled AND p.publicationDateStart <= :startDate AND p.collection = :collectionid AND r.id = :region AND p.id IN ('2','1') ORDER BY p.publicationDateStart DESC"
-parameters: ArrayCollection {#1500 ▶}
-_firstResult: null
-_maxResults: null
-joinRootAliases: array:3 [▶]
#cacheable: false
#cacheRegion: null
#cacheMode: null
#lifetime: 0
}
#sortBy: null
#sortOrder: null
#firstResult: 0
#maxResults: 10
#results: null
}
#countColumn: array:1 [▶]
}
这是树枝标记(略有缩写)
<div class="container news-list">
{% set posts = pager.getResults() %}
{% for post in posts %}
<div class="news-item">
// Mark up for post item
</div>
{% else %}
{{ 'no_post_found'|trans({}, 'SonataNewsBundle') }}
{% endfor %}
<ul class="pager">
<li class="previous">
<a class="btn {% if pager.page == pager.firstPage %} disabled{% endif %}"
{% if pager.page != pager.firstPage %}
href="{{ url(route, route_parameters|merge({'page': pager.previouspage})) }}"
{% endif %}
title="{{ 'link_previous_page'|trans({}, 'SonataNewsBundle') }}">
{{ 'link_previous_page'|trans({}, 'SonataNewsBundle') }}
</a>
</li>
<li class="next">
<a class="btn {% if pager.page == pager.lastPage %} disabled{% endif %}"
{% if pager.page != pager.lastPage %}
href="{{ url(route, route_parameters|merge({'page': pager.nextpage})) }}"
{% endif %}
title="{{ 'link_next_page'|trans({}, 'SonataNewsBundle') }}">
{{ 'link_next_page'|trans({}, 'SonataNewsBundle') }}
</a>
</li>
</ul>
</div>
如何让寻呼机显示正确的页面信息?
答案 0 :(得分:0)
解决方案原来是单线。在扩展寻呼机以添加标准的地方,需要重新初始化:
public function getPager(array $criteria, $page, $limit = 10, array $sort = array())
{
$criteria['date'] = [
'query' => 'p.publicationDateStart <= :startDate',
'params' => [
'startDate' => new \DateTime(),
]
];
$pager = parent::getPager($criteria, $page, $limit, $sort);
/** @var QueryBuilder $query */
$query = $pager->getQuery()->getQueryBuilder();
$parameters = $query->getParameters();
if (isset($criteria['title'])) {
$query->andWhere('p.title = :title');
$parameter = new Parameter('title', $criteria['title']);
$parameters->add($parameter);
}
if (isset($criteria['region'])) {
$query->leftJoin('p.regions', 'r');
$query->andWhere('r.id = :region');
$parameter = new Parameter('region', $criteria['region']);
$parameters->add($parameter);
}
/** THIS NEEDED TO HAPPEN IN ORDER TO RE-INITIALISE **/
$pager->init();
return $pager;
}