使用Doctrine ORM查询当天的生日

时间:2013-05-17 01:17:00

标签: orm doctrine-orm dql

使用原始SQL我习惯性地执行以下操作:

SELECT * FROM users WHERE MONTH(birthday) = 11 AND DAY(birthday) = 17

但是这些MONTH()DAY()函数不适用于所有数据库= /

那么,与Doctrine支持的任何数据库一起使用的最佳方法是什么?

1 个答案:

答案 0 :(得分:1)

要在SQL服务器级别进行此计算,必须创建一些自定义DQL函数:

http://www.doctrine-project.org/blog/doctrine2-custom-dql-udfs.html

看看官方文档:

http://doctrine-orm.readthedocs.org/en/latest/reference/dql-doctrine-query-language.html#adding-your-own-functions-to-the-dql-language

以下是postgresql DISTANCE函数的示例:

https://github.com/KnpLabs/DoctrineBehaviors/blob/master/src/Knp/DoctrineBehaviors/ORM/Geocodable/Query/AST/Functions/DistanceFunction.php

您关心的是为任何支持的RDBM做这项工作吗? 利用getSql()方法中的DatabasePlatform:

<?php

public function getSql(SqlWalker $walker)
{
    $walker->getConnection()->getDatabasePlatform(); // platform specific stuff
}