Introducing – ZF + D2
Greetings! Welcome to my probably wrong but hopefully right series on integrating Doctrine 2 with Zend Framework! I’m a huge fan of frameworks and object relational mappers (ORM’s) like many developers. Recently (okay, maybe not so recently) I started working on and immediately decided upon the Zend Framework along with Doctrine 2. I had previous experience with the Zend Framework and Doctrine 1 but I wanted to check out the hype surrounding Doctrine 2. Doctrine 1 was pain in the rear to integrate with Zend Framework but fortunately it’s pretty painless with Doctrine 2.
Getting Started
First off, let’s gather the required files. I’m going to assume you have a working Zend Framework project. If not, you can always check out the Zend Framework Quick Start tutorial.
- Download Doctrine 2 (BETA 4 at the time of writing)
- Extract Doctrine 2 to a folder within your project or PHP include path. I tend to use a vendor/ folder third-party libraries but the choice is yours. I’m going to assume you extracted to your /vendor folder
- Profit?
If you would prefer you can download the project from GitHub which has the above setup for you already.
Integrating the goodness
So, after three short steps you should have all the files in the appropriate places or have downloaded the sample project and can follow along. The first order of business is getting everything integrated nicely. We’re going to setup the Doctrine 2 as a re-usable Zend_Application resource plugin thanks to Matthew O’Phinney’s nifty guide.
My_Resource_Entitymanager
class My_Resource_Entitymanager extends Zend_Application_Resource_ResourceAbstract { // Default array of options that can be overridden using the application.ini file. // e.g., resources.entityManager.autoGenerateProxyClasses = false protected $_options = array( 'connection' => array( 'driver' => 'pdo_mysql', 'host' => 'localhost', 'dbname' => 'dbname', 'user' => 'root', 'password' => ''), 'modelDir' => '/models', 'proxyDir' => '/proxies', 'proxyNamespace' => 'Proxies', 'autoGenerateProxyClasses' => true ); public function init() { $options = $this->getOptions(); $config = new \Doctrine\ORM\Configuration; $cache = new \Doctrine\Common\Cache\ArrayCache; $driverImpl = $config->newDefaultAnnotationDriver($options['modelDir']); $config->setMetadataCacheImpl($cache); $config->setQueryCacheImpl($cache); $config->setProxyDir($options['proxyDir']); $config->setProxyNamespace($options['proxyNamespace']); $config->setAutoGenerateProxyClasses($options['autoGenerateProxyClasses']); $config->setMetadataDriverImpl($driverImpl); $em = \Doctrine\ORM\EntityManager::create($options['connection'], $config); return $em; } }
Wait a minute!
I know, I know, you’re probably thinking “How do we access the entity manager from the rest of the project?” Easy! Just access it like any other resource.
// To access in a controller use getInvokeArg() like so public function indexAction() { $em = $this->getInvokeArg('bootstrap')->getResource('entityManager'); } // To access elsewhere use the Zend_Controller_Front singleton public function myRandomMethodSomewhere() { $em = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('entityManager'); }
That’s all folks!
Well, not really, but for now. You can check back later for the other parts as I get them finished (and decide what to write). Cheers!
Hello,
nice tutorial.
I have one question, how can I integrate Zend_Session on DB with Doctrine 2.0 ?
Now I use default zend_db_adapter but if I use doctrine then users session data not writing to database.
Can you help me?
Best Regard
mits
You will need to create a custom session handler using session_set_save_handler(). There’s a tutorial over on the Dev Zone @ http://devzone.zend.com/article/141
I think it’s a bad practice to use the EntityManager from a controller, please check my blog post on better model-layer organization.
http://www.cobbweb.me/2010/11/integrate-doctrine-2-zend-framework-application/
I concur and specifically avoided going into details about it because it’s out of the scope of this article. I personally use a service layer to help keep my controllers nice and THIN.
I have been looking for a resource to integrate Doctrine 2 and Zend Framework, I am glad I found your site. Thanks so much for sharing!
Glad you like it!
[...] a little while to figure this one out… Trying to integrate ZF 1.11 with Doctrine2 to as per this article, I was trying to be a smart-ass and use TS_Resource_EntityManager instead of the ugly looking [...]
Hello SpiffyJr:
I have a question , in use Doctrine2 + Zend, I will download the file (contains two folders, bin and Doctrine), I put zend project library floder, I think it should be the relationship of namespace error, because I do not understand what should be added to code in Bootstrap file.
All of the code can be found in the project on GitHub.
Hi, as others I was looking for a way to integrate Doctrine 2 within a Zend Framework project.
Great work
By the way, I found a similar resource, Bgy_Application_Resource_Doctrine2, maybe you’ll be interested in!
Thanks again for your explanation.
Hey,
I’m v. new to doctrine, i dont want to sound like a ‘tard but i understand what you did above, what i dont get is how to actually use Doctrine.
I am looking at their tutorial hopefully this will clear things up. I would like to second the need for a service layer, for me it’s all about thin controllers and doing as much work within the service layer.
While in code I see declaration of \Doctrine\ORM\Configuration but if I run code I get:
Fatal error: Class ‘Doctrine\ORM\Configuration’ not found in C:\Program Files\Zend\Apache2\htdocs\MyProject\library\Custom\Entitymanager.php on line 21
Hi,
i’m getting the following error
Zend_Application_Bootstrap_Exception with message Resource
matching “entityManager” not found.
T,he first thing i change was the case, so its name now is “My_Resource_Entitymanager” (on /library/My/Resource/Entitimanager.php).
I change the pluginPath to pluginPaths.My_Resource = “My/Resource”.
But i am getting the error and i can’t fix it, i download you example project but i can’t find whats wrong.
Can you give me a hand?
thanks
It was a nice post indeed,I have started doctrine today.