gpt4 book ai didi

php - 按月分页 Symfony/Paginator

转载 作者:行者123 更新时间:2023-11-29 19:04:11 26 4
gpt4 key购买 nike

我正在使用分页器和 fosJsRouting 来制作带有过滤器的表单..

我尝试创建一个过滤器,仅根据所选月份显示结果。因此,在主页上,我们选择在 Controller 中花费的月份,然后我想在请求中即时传递它。我无法定义如果变量为空,则选择全部。谢谢您的帮助

这是我的 Controller 索引:


public function indexAction( Request $request, $page, $limit, $month ) {
$em = $this->getDoctrine()->getManager();

if(isset($_GET['month']))
$month = $_GET['month'];
else
$month = "NULL";

$debits = $em->getRepository( 'BudgetBundle:Debit' )->getAmounts($this->getUser(), $month);

// Paramerage de la limite d'affichage par page
if(isset($_GET['limit'])) $limit = $_GET['limit'];
##################################################################################### START_DEBIT
if ( $page == '' ) // Evite l'affichage de la page numero 2 sur l'index
{
$page = 1;
}
$paginator = $this->get( 'knp_paginator' );
$pagination = $paginator->paginate(
$debits, /* query NOT result */
$request->query->getInt( 'page', $page )/*page number*/,
$limit/*limit per page*/
);

// Calcul du Total de dépense par page
$total = [];
foreach ( $pagination->getItems() as $item ) {
$total[] = $item->getAmount();
}
$totalAmount = [];
for ( $i = 0; $i < count( $debits->getQuery()->getResult() ); $i ++ ) {

$totalAmount[] = $debits->getQuery()->getResult()[ $i ]->getAmount();
}
##################################################################################### END_DEBIT

return $this->render( 'BudgetBundle:Views:index.html.twig', [
'pagination' => $pagination,
'total' => array_sum( $total ),
'montantTotal' => array_sum( $totalAmount )
] );
}

查询生成器:

      public function getAmounts($user, $month){

$qb = $this->createQueryBuilder('d')
->leftJoin('d.company', 'company')
->addSelect('company')
->where('d.user = :user')
->setParameter('user', $user)
// ->andWhere( $month == NULL ? 'd.date = IS NOT NULL' : 'd.date = :month' )
// ->setParameter('month', $month)
->orderBy('d.date', 'DESC');

var_dump($month);
var_dump($qb->getQuery()->getDQL());
return $qb;
}

我的观点:

    <label for="month">Choisissez le mois</label>
<select title="month" class="form-control" style="display: inline;width: 100px; height: 30px;" name="month" id="month">

{% if app.request.query.get("month") %}
<option style="display: none;" value="{{ app.request.query.get("month") }}" selected>{{ app.request.query.get("month") }}</option>
{% endif %}
<option value="01">Janvier</option>
<option value="02">Fevrier</option>
</select>

Js:

    $('#month').change(function () {
var url = Routing.generate('budget_homepage', {'month':$('#month').find(":selected").text()});
window.location.href = url;
});

路线:

    budget_homepage:
path: /{page}
defaults: { _controller: BudgetBundle:Budget:index, page: 1, limit: 5, month: null }
requirements:
page: \d*
limit: \d*
options:
expose: true

最佳答案

要按月份过滤,您必须使用 Doctrine 扩展。

Doctrine extensions bundle

这个包向 Doctrine 添加了几个 Mysql 函数。

关于php - 按月分页 Symfony/Paginator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43638521/

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