Symfony2 - 尝试获取特定年份的帖子计数时出错

时间:2014-07-02 11:30:46

标签: symfony doctrine-orm count

我正在尝试创建查询以查找2014年内的所有帖子。

我有以下内容但出现错误:

[Syntax Error] line 0, col 88: Error: Expected end of string, got '00'

我的查询有什么问题?

public function getPostCountByYear()
{
    $query = $this
        ->createQueryBuilder('post')
        ->where('created >= 2014-01-01 00:00:00 AND created < 2015-01-01 00:00:00');
    $query->select('COUNT(post)');

    $year = $query
        ->getQuery()
        ->getSingleScalarResult();

    return $year;
}

1 个答案:

答案 0 :(得分:0)

试试这个。

public function getPostCountByYear()
{
    $query = $this
        ->createQueryBuilder('post')
        ->where('post.created >= :begin')
        ->andWhere('post.created < :end')
        ->setParameter('begin', new \DateTime('2014-01-01 00:00:00'))
        ->setParameter('end', new \DateTime('2015-01-01 00:00:00'));

    $query->select('COUNT(post.created)');

    $year = $query
        ->getQuery()
        ->getSingleScalarResult();

    return $year;
}