【问题标题】:Get data from previous month in Doctrine2在 Doctrine2 中获取上个月的数据
【发布时间】:2015-11-06 10:36:30
【问题描述】:

我需要上个月创建的表中的所有数据。我已经创建了下面的代码,但是它在 DQL 中给出了以下错误,但是运行它时 SQL 查询是正确的。

错误:

[Syntax Error] line 0, col 138: Error: Expected Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS, got '-' 

代码是;

$compCases = $this->getDoctrine()
            ->getRepository('EFuturesCrmBundle:CasesCompensation')
            ->createQueryBuilder('c')
            ->select('c.id')
            ->where('c.caseStatus =:status')
            ->andWhere('YEAR(c.caseStatusDate) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)')
            ->andWhere('MONTH(c.caseStatusDate) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)')
            ->setParameter('status', 'resolve')
            ->getQuery()
            ->getResult();

INTERVAL 不支持 DQL,那么这在 Doctrine2 中怎么可能?

【问题讨论】:

    标签: symfony doctrine-orm


    【解决方案1】:

    尝试使用这种方法:

     $now = new \DateTime('now');
     $delay = new \Datetime('last month');
    
     $compCases = $this->getDoctrine()
            ->getRepository('EFuturesCrmBundle:CasesCompensation')
            ->createQueryBuilder('c')
            ->select('c.id')
            ->where('c.caseStatus = :status')
            ->andWhere('c.caseStatusDate <= :now')
            ->andWhere('c.caseStatusDate >= :delay')
            ->setParameter('status', 'resolve')
            ->setParameter('now', $now)
            ->setParameter('delay', $delay)
            ->getQuery()
            ->getResult();  
    

    希望对你有帮助。

    【讨论】:

    • 这是解决问题的一部分。但是我想从今天获取数据。它应该是上个月最后一个日期到上个月第一个日期。我怎样才能得到上个月的最后日期而不是$now = new \DateTime('now');
    • 好的,例如:$from = new \DateTime('first day of last month 00:00:00'); $to = new \DateTime('first day of this month 00:00:00');
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 2021-10-10
    • 2023-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多