简体   繁体   English

如何在基于模块的应用程序中集成Doctrine 2和Zend Framework 1.12

[英]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). 我一直在寻找有关如何集成Doctrine 2和Zend Framework 1.12(或1.11或其他版本的解释),我真的不知道这是否重要,但我使用的是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 . 我可以在Stack Overflow的此处找到几篇博客文章,甚至解决问题,但是一劳永逸地读完之后,我一无所获: 在模块化应用程序中进行操作 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. 谢谢大家的答复,但是最近发布的ZF2使我决定离开ZF1,以便利用所有新的改进和功能。 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). 正如@KTastrophy所说,现在将ZF和Doctrine集成起来要容易得多(我什至敢说一切都更容易并且与ZF2更加一致)。 Thank you one more time! 再次感谢您!

It's easy to integrate doctrine 2 with ZF using the doctrine PEAR installation. 使用PEAR的PEAR安装,可以将Zept 2与ZF轻松集成。 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. $this->getOptions()从配置文件中检索数据库名称,用户和密码。

If you take this tutorial as an example 如果以本教程为例

http://christian.soronellas.es/2010/12/19/zend-framework-and-doctrine-2/?lang=en 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. 函数newDefaultAnnotationDriver实际上采用了一组实体路径。 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. 当我发现这一点时,我只是在每个模块中创建了一个实体文件夹,并沿着数组中的newDefaultAnnotationDriver参数传递了每个路径。 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 您应该应用此补丁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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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