【问题标题】:DQL wont accept dateDQL 不接受日期
【发布时间】:2017-03-02 09:49:40
【问题描述】:

我有这个存储库功能来选择与特定日期内的图像相关的所有投票:

public function getTopNImages(int $n)
    {

        $date = date_format(new \DateTime('first day of this month'), 'Y-m-d H:i:s');

        var_dump($date);

        $q2 = $this->createQueryBuilder('aliasi2')
            ->select('count(v.id)')
            ->innerJoin('aliasi2.votes', 'v')
            ->where('aliasi2 = i')
            ->andwhere("date_diff(v.date, $date) >= 0");

        return $this->createQueryBuilder('i')
            ->select(array(
                'i',
                '(' . $q2->getDQL() .') votes' 
            ))
            ->orderBy('votes', 'DESC')
            ->setMaxResults($n)
            ->getQuery()
            ->getResult();
    }

我使用的日期格式与 mysql 中的相同,var_dump 输出如下:

字符串(19)“2017-03-01 09:39:34”

但由于某种原因,我得到了这个:

[语法错误] line 0, col 137: Error: Expected Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS, got '-'

这可能是什么问题?

【问题讨论】:

    标签: php mysql doctrine-orm symfony mariadb


    【解决方案1】:

    您忘记用简单的引号保护您的日期,因此词法分析器在第一个“-”处失败。

    在这里查看问题:

    "date_diff(v.date, 2017-03-01 09:39:34) >= 0"
    

    你需要这样做:

    "date_diff(v.date, '$date') >= 0"
    

    或者甚至更好地使用 setParameter()

    【讨论】:

    • 它给了我1 parameters expected 0 set
    • 这是因为 setParameter() 必须在主查询中,而不是在子查询中:)
    【解决方案2】:

    你试过了吗:

    $q2 = $this->createQueryBuilder('aliasi2')
            ->select('count(v.id)')
            ->innerJoin('aliasi2.votes', 'v')
            ->where('aliasi2 = i')
            ->andwhere("date_diff(v.date, :date) >= 0")
            ->setParameter('date', $date);
    

    这只是设置了日期参数...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-25
      • 1970-01-01
      • 1970-01-01
      • 2021-03-31
      • 2014-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多