Archive for November, 2010

Zend Framework 1.11 + Doctrine 2 – Let’s play nice, part 1

Wednesday, November 17th, 2010

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.

  1. Download Doctrine 2 (BETA 4 at the time of writing)
  2. 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
  3. 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!