Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine DQL query with SQL functions

I'm porting simple web application written in CodeIgniter to Symfony2 bundle. I'm new into Symfony2 and Doctrine and I've a problem with one SQL query, that I want to rewrite in 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. Unfortunately, I have no idea how to implement this SQL query in DQL:

$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. 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. So I would like to have this done in OOP way or using clever DQL query that understands something like:

$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. Please, help me finding right solution.

like image 255
Darrarski Avatar asked Feb 03 '12 09:02

Darrarski


People also ask

What is Doctrine query?

Doctrine Query Language (DQL) is an Object Query Language created for helping users in complex object retrieval. You should always consider using DQL (or raw SQL) when retrieving relational data efficiently (eg. when fetching users and their phonenumbers).

What is doctrine SQL?

Doctrine features a powerful query builder for the SQL language. This QueryBuilder object has methods to add parts to an SQL statement. If you built the complete state you can execute it using the connection it was generated from. The API is roughly the same as that of the DQL Query Builder.

What is the difference between SQL and Dql?

Doctrine Query Language (DQL) is like an abstraction of your queries. This makes queries independent of the version or type of (SQL) database that your project is built on. From the documentation: You need to think about DQL as a query language for your object model, not for your relational schema.

What are the data query retrieval language?

Data Query Language (DQL) is part of the base grouping of SQL sub-languages. These sub-languages are mainly categorized into four categories: a data query language (DQL), a data definition language (DDL), a data control language (DCL), and a data manipulation language (DML).


1 Answers

I know I meets a little late but if this allows the other to find an alternative solution :

use bundle : Doctrine Extensions

after configure your config.yml :

doctrine:
    orm:
        dql:
            string_functions:
                UNIX_TIMESTAMP: DoctrineExtensions\Query\Mysql\UnixTimestamp
like image 180
Snoozer Avatar answered Sep 19 '22 01:09

Snoozer