views:

2478

answers:

3

I'm interested in using Doctrine as an ORM for a new Zend Framework app I'm writing. I'm trying to figure out the best way to integrate it as straightforward as possible. Every example I find is different, and a lot of them pre-date the new autoloading features in ZF 1.8. None of them have worked for me yet.

Does anyone have a good way to do this? I'm inclined to want to place it in my bootstrap file, but some people suggest making a Zend_Application_Resource plugin. The hard part seems to be getting the load paths working correctly for both the Doctrine namespace and the model classes which by default don't follow the Zend auto-loading convention.

Any thoughts? Thanks.

A: 

As far as auto-loading is concerned, you can actually use the Doctrine loader with the new Zend_Loader_Autoloader stack quite easily. Take a look at this page, especially where it mentions the pushAutoloader() method.

Here's the basic run down, though:

$autoloader = Zend_Loader_Autoloader->getInstance();
$autoloader->pushAutoloader(array('Doctrine', 'autoload'), 'Doctrine');

This will use Doctrine's own autoloader for only classes that begin with Doctrine, if they are not already found by other autoloaders in the stack.

Hope this helps a bit.

jason
+3  A: 

I wrote a Resource Bootstrapper for Doctrine and Zend Framework a few weeks ago and turned it all into a small wrapper framework, cause I think ZF and Doctrine are a great team. You can read the article here: http://coffeecoders.de/2009/06/using-the-zend-framework-18-bootstrapper-and-doctrine-110/

It is fully configurable via the Bootstrap resource configurations (example included, too). Unfortunately Doctrine searches for Models in the model folder with the same classname as the filename (which doesn't match the ZF naming scheme) so it was actually not possible to get rid of registering the Doctrine Autoloader. The resource Loader looks like this:

<?php
/**
 * Doctrine model loading bootstrap resource. Options must provide a connection string.
 * directory option for model directory is optional (default is ./models).
 * Further options will be set for the Doctrine manager via setAttribute (e.g. model_loading). 
 * @author daff
 */
class Cuckoo_Application_Resource_Model extends Zend_Application_Resource_ResourceAbstract
{
    public function init()
    {
     $manager = Doctrine_Manager::getInstance();
     $options = $this->getOptions();

     foreach($options as $key => $value)
     {
           if($key != 'connection' && $key != 'directory')
                    $manager->setAttribute($key, $value);
     }

     if(empty($options['connection']))
      throw new Exception("No database connection string provided!");
     Doctrine_Manager::connection($options['connection']);
     if(empty($options['directory']))
      $dir = './models';
     else
      $dir = $options['directory'];
     Doctrine::loadModels(realpath($dir));
     return $manager;
    }
}
Daff
Thanks. I ended up going with a similar approach. Matthew Lurz has written [proposal](http://framework.zend.com/wiki/display/ZFPROP/Zend_Application_Resource_Doctrine+-+Matthew+Lurz) for a ZendX resource plugin. He's pasted some initial [specifications](http://pastie.org/481635) and [code](http://pastie.org/481633). It's similar to what you have, and solved my problems.
Bryan M.
+1  A: 

http://weierophinney.net/matthew/archives/220-Autoloading-Doctrine-and-Doctrine-entities-from-Zend-Framework.html

take a look at this post. It gives a detailed explanation, directory structure and how to use autaloading features.

radalin