9 Mar 2011

Integrating Doctrine2 ORM in Zend Framework project

Hello everyone.

This is my first try to write post in english. The main reason for that is beacause I believe that it really can be usefull to more than just russian speaking readers. So don't judje my english too strict. =)

Last month I had a chance to try Doctrine2 and all the cool stuff implemented there. So I need to integrate it into Zend Framework project. It turns out to be not a very difficult task. There are already many manuals all over the internet and you can easily find them in Google. But I'll also share some usefull links at the end of the post.

So, let's begin. First, download Doctrine and put it in the library/Doctrine folder of the modular zf project.

Then comes the bootstrap.

Several important notes here.

Doctrine 2 uses namespaces heavily. That's why we need to add separate autoloaders besides main Zend's autoloader. One for Doctrine classes, one for Symfony components and one for our model entities. I put models in application/model/Entities folder (example of the model later in this post).

In the development environment I use ArrayCache() and Memcached through MemcacheCache() in the staging and production environments.

For Doctrine mapping I decided to use annotaions, that's why we need to set up corresponding driver.

EntityManager is the main object here, we are going to use it all over the project. So it's a good idea to put it in the Registry.

Here are important configuration options from application.ini.

At this point you're almost done and can easily access entity manager or your models from anywhere in your project. There is of course other ways to integrate Doctrine. For example using resource plugin and more configuration options in application.ini. But for the moment I found bootstrap way much easier and faster to implement. Ok, now let's take a look at the model example.

Nothing complex here. All the stuff to understand annotations you can find in the Doctrine documentation, it is really good and easy to search through.

Now, we need some convenient way to access entity manager from our controllers. Of course you can just get it from the registry, but i wanted action helper. It's just looks cleaner and more logical here. You can pass manager to it directly through constructor while instantiating helper in bootstrap. Here I just get it from the Registry.

Finally, let's see how it will act like in controller.

Usually I don't do stuff like that directly in the controller (fat controllers sucks) prefering Service Layer instead. It is easy to refactor and pass entity manager to service class and then do whatever you want there. It is always so with Zend: you can accomplish things differently depending on what and how you like more.

I promissed you the links for sources where I found huge pieces of information about the topic. So here ther are:

That's it. Hope you'll find this information usefull. I'll appreciate your feedback both on the subject of the post and my english skills.