Integrate Doctrine with Zend Framework 1.8 app

前端 未结 3 681
夕颜
夕颜 2021-01-13 17:24

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.

相关标签:
3条回答
  • 2021-01-13 18:16

    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;
        }
    }
    
    0 讨论(0)
  • 2021-01-13 18:24

    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.

    0 讨论(0)
  • 2021-01-13 18:26

    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.

    0 讨论(0)
提交回复
热议问题