简体   繁体   中英

How to integrate Doctrine 2 and Zend Framework 1.12 in a module based application

I've been looking for an explanation on how to integrate Doctrine 2 and Zend Framework 1.12 (or 1.11, or another --I don't really know whether it matters or not but what I'm using is 1.12). I could find several blog posts and even solved questions right here in Stack Overflow but after read them one and all, I couldn't get to get what I was after: do it in a modular application . So, I'd be very grateful if somebody could give me the keys to achieve this.

Thank you very much!

EDIT:
Thank you guys for your replies but the recent release of ZF2 made me to decide to leave ZF1 in order to take advantage of all new improvements and features. As @KTastrophy said, integrating ZF and Doctrine is quite much easier now (I'd even dare to say that everything is easier and more consistent with ZF2). Thank you one more time!

It's easy to integrate doctrine 2 with ZF using the doctrine PEAR installation. After installing you just need to put this in your bootstrap:

protected function _initDoctrine() {
    require_once "Doctrine/ORM/Tools/Setup.php";
    \Doctrine\ORM\Tools\Setup::registerAutoloadPEAR();

    $options = $this->getOptions();

    $loader = new \Doctrine\Common\ClassLoader('YourNamespace', realpath(APPLICATION_PATH . "/../library"));
    $loader->register();


    $isDevMode = (APPLICATION_ENV == 'production') ? false: true;
    $entityManager = \Doctrine\ORM\EntityManager::create(
        $options['doctrine']['dbal'],
        \Doctrine\ORM\Tools\Setup::createYAMLMetadataConfiguration(array(
            realpath(APPLICATION_PATH."/../library/YourNamespace/Yaml"),
        ), $isDevMode)
    );

    Zend_Registry::set('entityManager', $entityManager);

    return $entityManager;
}

The $this->getOptions() retrieves the database name, user and password from the config file.

If you take this tutorial as an example

http://christian.soronellas.es/2010/12/19/zend-framework-and-doctrine-2/?lang=en

See this part of the configuration code

$config = new Configuration();         
$config -> setMetadataCacheImpl($cache);         
$driverImpl = $config -> newDefaultAnnotationDriver($options['entitiesPath']);         
$config -> setMetadataDriverImpl($driverImpl);        
 $config -> setQueryCacheImpl($cache);        
 $config -> setProxyDir($options['proxiesPath']);         
$config -> setProxyNamespace('Application\Models\Proxies');         
$config -> setAutoGenerateProxyClasses(('development' == APPLICATION_ENV));        
 $em = EntityManager::create(             $this -> _buildConnectionOptions($options),             $config        );

The function newDefaultAnnotationDriver actually takes an array of entitites path. This creates the opportunity for you to get creative. When I found out about this, I simply created an entity folder in each module and pass each path along the newDefaultAnnotationDriver parameter in an array. Ofcourse by doing this, you will need to set the namespace per module.

I use Bisna

You should apply this patch https://github.com/guilhermeblanco/ZendFramework1-Doctrine2/pull/45

And that works well for me.

In the controller I have this function for retrieve the Entity Manager

/**
 * Retrieve the Doctrine Container.
 *
 * @return Doctrine\ORM\EntityManager
 */
public function getEntityManager()
{
    return $this->getInvokeArg('bootstrap')->getResource('doctrine')->getEntityManager();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM