php - 按月分页 Symfony/Paginator

标签 php mysql symfony doctrine

我正在使用分页器和 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/

相关文章:

PHP 检查 mysql 连接

javascript - opencart 2.0 中搜索按钮移至顶部栏

php - 带有 lat lng 和多个搜索连接的 mysql 查询

php - $foo === TRUE 和 TRUE === $foo 有什么区别

php - 如何启动 PHP?

mysql - nodejs mysql错误: Connection lost The server closed the connection

php - 使用 PDO 插入时获取自动递增行 ID

symfony - 具有键 "getPageTitle"的数组的键 "0"不存在

Symfony - 在 Doctrine EntityRepository 中注入(inject)和访问 DIC 服务

php - 插入新游戏时出现 Symfony 错误 : Entities passed to the choice field must be managed. 也许将它们保留在实体管理器中?