我想执行一个在本机SQL中看起来像这样的查询:
SELECT AVG(t.column) AS average_value FROM table t WHERE YEAR(t.timestamp) = 2013 AND MONTH(t.timestamp) = 09 AND DAY(t.timestamp) = 16 AND t.somethingelse LIKE 'somethingelse' GROUP BY t.somethingelse;
如果我想像这样在Doctrine的查询生成器中实现这一点:
$qb = $this->getDoctrine()->createQueryBuilder(); $qb->select('e.column AS average_value') ->from('MyBundle:MyEntity', 'e') ->where('YEAR(e.timestamp) = 2013') ->andWhere('MONTH(e.timestamp) = 09') ->andWhere('DAY(e.timestamp) = 16') ->andWhere('u.somethingelse LIKE somethingelse') ->groupBy('somethingelse');
我收到错误异常
[语法错误]第0行,第63行:错误:预期的已知功能,获取了’YEAR’
如何使用Doctrines查询构建器实现查询?
笔记:
YEAR()
您可以添加Doctrine扩展名,以便在Symfony上通过添加以下配置来使用MySql YEAR和MONTH语句:
YEAR
MONTH
doctrine: orm: dql: string_functions: MONTH: DoctrineExtensions\Query\Mysql\Month YEAR: DoctrineExtensions\Query\Mysql\Year
现在,您可以在DQL或querybuilder中使用MONTH和YEAR语句。