如何在 Doctrine2 中使用 SQL 的 YEAR()、MONTH() 和 DAY()?

2023-06-01数据库问题
3

本文介绍了如何在 Doctrine2 中使用 SQL 的 YEAR()、MONTH() 和 DAY()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想在本机 SQL 中执行如下所示的查询:

I want to perform a query which would look like this in native 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 的查询构建器中实现这个:

If I am trying to implement this in Doctrine's query builder like this:

$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'

[Syntax Error] line 0, col 63: Error: Expected known function, got 'YEAR'

如何使用 Doctrines 查询构建器实现我的查询?

How can I implement my query with Doctrines query builder?

注意事项:

  • 我了解 Doctrine 的原生 SQL.我已经尝试过这个,但它导致了我的生产数据库表和我的开发数据库表具有不同名称的问题.我想在数据库不可知的情况下工作,所以没有选择.
  • 虽然我想工作与数据库无关:仅供参考,我使用的是 MySQL.
  • 有一种方法可以扩展 Doctrine 以学习"YEAR() 等语句,例如如见此处.但我正在寻找一种避免包含第三方插件的方法.
  • I know about Doctrine's Native SQL. I've tried this, but it leads to the problem that my productive and my development database tables have different names. I want to work database agnostic, so this is no option.
  • Although I want to work db agnostic: FYI, I am using MySQL.
  • There is way to extend Doctrine to "learn" the YEAR() etc. statements, e.g. as seen here. But I am looking for a way to avoid including third party plugins.

推荐答案

你可以添加Doctrine extension 所以如果您使用的是 Symfony,则可以通过添加此配置来使用 MySql YEARMONTH 语句:

You can add Doctrine extension so you can use the MySql YEAR and MONTH statement by adding this configuration if you're on Symfony:

doctrine:
    orm:
        dql:
            string_functions:
                MONTH: DoctrineExtensions\Query\Mysql\Month
                YEAR: DoctrineExtensions\Query\Mysql\Year

现在您可以在 DQL 或查询构建器中使用 MONTH 和 YEAR 语句.

now you can use the MONTH and YEAR statements in your DQL or querybuilder.

注意:扩展支持 MySQL、Oracle、PostgreSQL 和 SQLite.

Note: The extension supports MySQL, Oracle, PostgreSQL and SQLite.

这篇关于如何在 Doctrine2 中使用 SQL 的 YEAR()、MONTH() 和 DAY()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

Mysql目录里的ibtmp1文件过大造成磁盘占满的解决办法
ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致...
2025-01-02 数据库问题
151

按天分组的 SQL 查询
SQL query to group by day(按天分组的 SQL 查询)...
2024-04-16 数据库问题
77

SQL 子句“GROUP BY 1"是什么意思?意思是?
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)...
2024-04-16 数据库问题
62

MySQL groupwise MAX() 返回意外结果
MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)...
2024-04-16 数据库问题
13

MySQL SELECT 按组最频繁
MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)...
2024-04-16 数据库问题
16

在 Group By 查询中包含缺失的月份
Include missing months in Group By query(在 Group By 查询中包含缺失的月份)...
2024-04-16 数据库问题
12