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:
- http://stackoverflow.com/questions/3246515/using-doctrine-2-with-zend-framework-1-10-x
- http://jeboy25.blogspot.com/2010/08/doctrine-2-and-zend-framework-110.html
- http://www.doctrine-project.org/docs/orm/2.0/en/tutorials/getting-started-xml-edition.html
- http://stackoverflow.com/questions/4582203/unable-to-load-doctrine-orm-configuration-class-with-doctrine-2-classloader-in
- http://bostjan.muha.cc/2010/08/doctrine-2-zend-framework/
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.



Comments 16 Comments
I'm a Brazilian PHP Web Developer and I'm glad to find your website. So, how you see, write in English helps much more than just natives. Thanks heaps for your posts, I'm going through all of them. Success for you!! Cheers.
I have a question, which directory do you store the action helper in for the entity manager?
Look forward to hearing from you soon.
I put action helper in the library/My/Controller/Action/Helper directory, where "My" is namespace for my own components or extensions for zend framework.
Please forgive my ignorance, I'm new to Zend. One more question, how did you instantiate the helper in the bootstrap?
Many Thanks.
I just add a prefix to helper:
Zend_Controller_Action_HelperBroker::addPrefix('My_Controller_Action_Helper');
I have the following code in a controller:
$em = $this->_helper->Em();
$query = $em->createQuery('SELECT g FROM \Entities\Groups g');
$aGroups = $query->getResult();
However it throws the error: Message: [Semantical Error] line 0, col 14 near '\Entities\Groups()': Error: Class 'Entities\Groups' is not defined.
Any idea why this is?
Sincerely appreciate your help.
An error occurred
Application error
Exception information:
Message: Action Helper by name Em not found
Stack trace:
#0 E:\wamp\www\frameworks\Zend\Controller\Action\HelperBroker.php(293): Zend_Controller_Action_HelperBroker::_loadHelper('Em')
#1 E:\wamp\www\frameworks\Zend\Controller\Action\HelperBroker.php(323): Zend_Controller_Action_HelperBroker->getHelper('Em')
#2 E:\wamp\www\square\application\modules\default\controllers\IndexController.php(21): Zend_Controller_Action_HelperBroker->__call('Em', Array)
#3 E:\wamp\www\square\application\modules\default\controllers\IndexController.php(21): Zend_Controller_Action_HelperBroker->Em()
#4 E:\wamp\www\frameworks\Zend\Controller\Action.php(516): IndexController->indexAction()
#5 E:\wamp\www\frameworks\Zend\Controller\Dispatcher\Standard.php(295): Zend_Controller_Action->dispatch('indexAction')
#6 E:\wamp\www\frameworks\Zend\Controller\Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#7 E:\wamp\www\frameworks\Zend\Application\Bootstrap\Bootstrap.php(97): Zend_Controller_Front->dispatch()
#8 E:\wamp\www\frameworks\Zend\Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#9 E:\wamp\www\square\public\index.php(26): Zend_Application->run()
#10 {main}
Request Parameters:
array (
'controller' => 'index',
'action' => 'index',
'module' => 'default',
)
Repository Github :
https://github.com/shadowfree/ZF-tutorial-d2
Thanks for your help :)
Zend_Controller_Action_HelperBroker::addPrefix('My_Controller_Action_Helper');
First tutorial that actually helps!
Thank you!