Featured Post

How to copy many files with certain mask under Linux?

This time I would like to share solution for the problem described in the title of this post: how to copy many files with certain mask under the Linux Operating System. Background Let’s say I have 200 files with following types: <number>_400x300.<extension> – e.g. 534_400x300.jpeg, <number>_300x90.<extension>...

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