Featured Post

Integrating FirePHP with Zend Framework 2 using Composer

Introduction If you previously integrated FirePHPCore with Zend Framework using this description just remove require_once(realpath(__DIR__ . ‘/../../vendor/firephp/FirePHPCore/FirePHP.php’)); instance, where you include FirePHP library in global.php Modifying Composer configuration Now you...

Read More

Writing DQL statements

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

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

0

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:
https://github.com/evolic/zf2-tutorial/tree/release-0.3.1

Write a comment