简体   繁体   English

带有SQL函数的Doctrine DQL查询

[英]Doctrine DQL query with SQL functions

I'm porting simple web application written in CodeIgniter to Symfony2 bundle. 我正在将用CodeIgniter编写的简单Web应用程序移植到Symfony2包中。 I'm new into Symfony2 and Doctrine and I've a problem with one SQL query, that I want to rewrite in DQL. 我是Symfony2和Doctrine的新手,我遇到一个SQL查询问题,我想在DQL中重写。 I've all ready to go in my bundle, I've created Entity class and I'm able to insert data to database and do simple queries in Object-Oriented-Programming way that Symfony2 provides. 我已准备好进入我的捆绑包,我已经创建了Entity类,我能够将数据插入到数据库中,并以Symfony2提供的面向对象编程方式进行简单查询。 Unfortunately, I have no idea how to implement this SQL query in DQL: 不幸的是,我不知道如何在DQL中实现这个SQL查询:

$sql = "SELECT * FROM t WHERE 
UNIX_TIMESTAMP(t.date) > ".(time()-300)." AND
ROUND(t.x,3) = ".round($x, 3);

As you can see there are some SQL function calls, that needs to be executed on database server. 正如您所看到的,有一些SQL函数调用需要在数据库服务器上执行。 Doctrine can't understand this calls. 主义无法理解这种呼唤。 For sure, I have an option to quit using Doctrine and do this query using basic PDO inside my Symfony2 bundle, but I would like to take full advantages of using Symfony2 and Doctrine. 当然,我可以选择退出使用Doctrine并使用我的Symfony2包中的基本PDO进行此查询,但我想充分利用Symfony2和Doctrine。 So I would like to have this done in OOP way or using clever DQL query that understands something like: 因此,我希望以OOP方式完成此操作,或者使用能够理解类似内容的智能DQL查询:

$em->createQuery("SELECT t FROM MyTestBundle:MyEntity t WHERE t.x = :x")
->setParameter("x", round($x,3));

but being able to rewrite my SQL query from old application to my new bundle is a must. 但是能够将我的SQL查询从旧应用程序重写到我的新包是必须的。 Please, help me finding right solution. 请帮我找到正确的解决方案。

I know I meets a little late but if this allows the other to find an alternative solution : 我知道我遇到的有点迟了但是如果这允许另一个人找到另一种解决方案:

use bundle : Doctrine Extensions 使用bundle: Doctrine Extensions

after configure your config.yml : 配置config.yml之后:

doctrine:
    orm:
        dql:
            string_functions:
                UNIX_TIMESTAMP: DoctrineExtensions\Query\Mysql\UnixTimestamp

Unfortunately you can't use SQL functions in DQL query. 遗憾的是,您无法在DQL查询中使用SQL函数。 So you have 2 options: 所以你有两个选择:

  1. You can create a User Defined Function in DQL. 您可以在DQL中创建用户定义的函数。 Here is the official documentation of Doctrine on how to do this http://docs.doctrine-project.org/en/latest/cookbook/dql-user-defined-functions.html 以下是Doctrine如何执行此操作的官方文档http://docs.doctrine-project.org/en/latest/cookbook/dql-user-defined-functions.html

  2. Or you can execute an SQL query directly in doctrine. 或者您可以直接在doctrine中执行SQL查询。 Here is official documentation for using Native SQL in Doctrine http://docs.doctrine-project.org/en/latest/reference/native-sql.html 以下是在Doctrine中使用Native SQL的官方文档http://docs.doctrine-project.org/en/latest/reference/native-sql.html

Judging by the date on the question I don't think this can help you now but I hope it can help others that have the same problem 从问题的日期来看,我不认为这可以帮助你,但我希望它可以帮助那些有同样问题的人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM