How can I use SQL's YEAR(), MONTH() and DAY() in Doctrine2?

Gottlieb Notschnabel

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;

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');

I get the error exception

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

How can I implement my query with Doctrines query builder?

Notes:

  • 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.
alex88

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

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

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

来自分类Dev

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

来自分类Dev

如何在Doctrine2中选择YEAR()MONTH()DAY()

来自分类Dev

Joda Time - Day of month and Month of Year Not returning 2 digit output

来自分类Dev

使用纯 SQL 从 foo.year、foo.month、foo.day 转换为日期?

来自分类Dev

使用javascript将month-day-year转换成day-month-year

来自分类Dev

格式为{day-month-year}的单独日期

来自分类Dev

select_day / year / month预设值

来自分类Dev

How can I convert an Integer to month name in Groovy?

来自分类Dev

将Java中的时间格式从YYYY-MM-DD转换为Day-Month-Year

来自分类Dev

如何将分隔的Year(int),Month(int)和Day(int)列转换为DateTime

来自分类Dev

正则表达式从year-month-day-title.ext捕获标题

来自分类Dev

Extracting "Year" , "Month" and "Day" from Date column which is in continuous string format

来自分类Dev

ggplot“ day-month” x轴,带多面环绕式“ year”

来自分类Dev

mongodb聚合:在输出中连接_id.day,_id,month,_id.year

来自分类Dev

如何将chrono :: year_month_day添加到chrono :: sys_seconds

来自分类Dev

如何将日期格式化为以下格式day.month.year?

来自分类Dev

错误:在“class date::year_month_day”中没有名为“_traits”的类型?

来自分类Dev

熊猫时间序列重采样:KeyError:“['year''month''day']不在索引中”

来自分类Dev

如何使用正则表达式删除名称为 YEAR-MONTH-DAY 的文件

来自分类Dev

How can I call super() so it's compatible in 2 and 3?

来自分类Dev

Day and Year 交换了 SSIS 和 SQL Server

来自分类Dev

How can I use the constraints in native webrtc?

来自分类Dev

How can I save a <template> for a second use?

来自分类Dev

SWIFT: How I can use subscriberCellularProviderDidUpdateNotifier with Swift

来自分类Dev

How can I create this complicated SQL query?

来自分类Dev

试图将日期列表放入date.year,date.month,date.day中以便进行比较?

来自分类Dev

试图将日期列表放入date.year,date.month,date.day中以便进行比较?

来自分类Dev

Convert day of the year to date

Related 相关文章

  1. 1

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

  2. 2

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

  3. 3

    如何在Doctrine2中选择YEAR()MONTH()DAY()

  4. 4

    Joda Time - Day of month and Month of Year Not returning 2 digit output

  5. 5

    使用纯 SQL 从 foo.year、foo.month、foo.day 转换为日期?

  6. 6

    使用javascript将month-day-year转换成day-month-year

  7. 7

    格式为{day-month-year}的单独日期

  8. 8

    select_day / year / month预设值

  9. 9

    How can I convert an Integer to month name in Groovy?

  10. 10

    将Java中的时间格式从YYYY-MM-DD转换为Day-Month-Year

  11. 11

    如何将分隔的Year(int),Month(int)和Day(int)列转换为DateTime

  12. 12

    正则表达式从year-month-day-title.ext捕获标题

  13. 13

    Extracting "Year" , "Month" and "Day" from Date column which is in continuous string format

  14. 14

    ggplot“ day-month” x轴,带多面环绕式“ year”

  15. 15

    mongodb聚合:在输出中连接_id.day,_id,month,_id.year

  16. 16

    如何将chrono :: year_month_day添加到chrono :: sys_seconds

  17. 17

    如何将日期格式化为以下格式day.month.year?

  18. 18

    错误:在“class date::year_month_day”中没有名为“_traits”的类型?

  19. 19

    熊猫时间序列重采样:KeyError:“['year''month''day']不在索引中”

  20. 20

    如何使用正则表达式删除名称为 YEAR-MONTH-DAY 的文件

  21. 21

    How can I call super() so it's compatible in 2 and 3?

  22. 22

    Day and Year 交换了 SSIS 和 SQL Server

  23. 23

    How can I use the constraints in native webrtc?

  24. 24

    How can I save a <template> for a second use?

  25. 25

    SWIFT: How I can use subscriberCellularProviderDidUpdateNotifier with Swift

  26. 26

    How can I create this complicated SQL query?

  27. 27

    试图将日期列表放入date.year,date.month,date.day中以便进行比较?

  28. 28

    试图将日期列表放入date.year,date.month,date.day中以便进行比较?

  29. 29

    Convert day of the year to date

热门标签

归档