Featured Post

Polish Constitution of May 3, 1791

Today, on 3rd May 2014 we are celebrating national holiday in Poland, made in commemoration of Polish Constitution of May 3, 1791, called: Polish: Konstytucja Trzeciego Maja, Belarusian: Канстытуцыя трэцяга мая, Lithuanian: Gegužės trečiosios Konstitucija. Poland was placed...

Read More

Writing DQL statements

Posted by Tomasz Kuter | Posted in Programming, Web development | Posted on 21-03-2013

Tags: , , , , , , , , , ,


Today I would like to focus on something, what Doctrine 1.x users know, but it looks a little bit different – Doctrine Query Language (DQL) statements.

In the last tutorial we had had working Album Controller, which was using Doctrine Entity Manager, to get the Album data. Now we will focus on something more advanced – writing SQL alike statements to get some data from the album’s table.

So lets begin…

Moving database operations to model

Firstly we need to move model part to separate file /module/Album/src/Album/Model/Album.php:

And update Album controller (/module/Album/src/Album/Controller/AlbumController.php):

You can run the website and nothing should be changed – it should works as before.

Writing Doctrine Query Language (DQL) statements

Now let’s take the main part of this article – write some DQL.
Doctrine Query Language is very similar to SQL, so you should learn it quite fast.

We start with getting all albums in DQL now:

It haven’t changed a lot, so it should be clear.
Instead of table name we specify the Doctrine Entity class.

If you have FirePHP configured you could check DQL and SQL of the above statement:

More about integrating FirePHP you can find my my another post.

Now it is time for getting single album:

And this is it – you can try to use another DQL statements.
More about Doctrine Query Language you can find here.

All the code from this tutorial you can find at the GitHub:

Write a comment