gpt4 book ai didi

php - Symfony2 DatagridBundle/SonataNewsBundle Pager nbResults 计算不正确

转载 作者:搜寻专家 更新时间:2023-10-31 21:51:58 32 4
gpt4 key购买 nike

我扩展了 Sonata NewsBundle 以包含一个新闻区域。这是为了向英国游客展示英国新闻,向美国游客展示美国新闻等。

尽管在 Pager 对象中使用的查询是正确的,但 nbResults 计数和页面正在计算所有区域的新闻 - 而不仅仅是查询中的区域。这导致页数不正确。

pager.getResults() 正在获取正确的帖子。这是寻呼机对象的 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 [▶]
}

这是 Twig 标记(略有缩写)

<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>

如何让寻呼机显示正确的页面信息?

最佳答案

结果证明是单线解决方案。在寻呼机被扩展以添加标准的地方,它需要重新初始化:

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;
}

关于php - Symfony2 DatagridBundle/SonataNewsBundle Pager nbResults 计算不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40103666/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com