整合ZF / Doctrine2:我将模型/实体和代理类 [英] Integrating ZF/Doctrine2: Where do i put my Models/Entities & Proxy classes

查看:88
本文介绍了整合ZF / Doctrine2:我将模型/实体和代理类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将Zend Framework 1.10与Doctrine 2整合在一起,那么我将Doctrine Model / Entities和Proxies放在哪里?我想到了 / application / library 目录。如果我放在 / library 目录下,它会干扰ZF自动加载类,因为类将使用PHP 5.3名称空间与PEAR样式命名空间。 p>

解决方案

我正在研究将Doctrine 2与ZF1.10整合在一起的应用程序。您不需要使用Doctrine自动1)在您的application.ini文件中添加以下行(假设您的库文件夹中安装了Doctrine(与Zend文件夹相同):



/ p>

  autoloadernamespaces.doctrine =Doctrine

2)创建一个原则或实体管理器资源。在您的ini文件中:

  resources.entitymanager.db.driver =pdo_mysql
resources.entitymanager.db。 user =user
resources.entitymanager.db.dbname =db
resources.entitymanager.db.host =localhost
resources.entitymanager.db.password =pass
resources.entitymanager.query.cache =Doctrine\Common\Cache\ApcCache
resources.entitymanager.metadata.cache =Doctrine\Common\Cache\ApcCache
resources.entitymanager.metadata.driver =Doctrine\ORM\Mapping\Driver\AnnotationDriver
resources.entitymanager.metadata.proxyDir = APPLICATION_PATH/../data/proxies
resources.entitymanager.metadata.entityDir [] = APPLICATION_PATH/ models / entity

3)下一页,你将需要引导它。我在资源文件夹中添加了一个资源类。确保您映射到ini文件中的文件夹:

  pluginPaths.Application_Resource_ = APPLICATION_PATH/ resources

然后你的资源类...

 code> class Application_Resource_EntityManager 
扩展Zend_Application_Resource_ResourceAbstract
{
/ **
* @var Doctrine\ORM\EntityManager
* /
protected $ _em;

public function init()
{
$ this-> _em = $ this-> getEntityManager();
return $ this-> _em;
}

public function getEntityManager()
{
$ options = $ this-> getOptions();
$ config = new \Doctrine\ORM\Configuration();
$ config-> setProxyDir($ options ['metadata'] ['proxyDir']);
$ config-> setProxyNamespace('Proxy');
$ config-> setAutoGenerateProxyClasses((APPLICATION_ENV =='development'));
$ driverImpl = $ config-> newDefaultAnnotationDriver($ options ['metadata'] ['entityDir']);
$ config-> setMetadataDriverImpl($ driverImpl);
$ cache = new Doctrine\Common\Cache\ArrayCache();
$ config-> setMetadataCacheImpl($ cache);
$ config-> setQueryCacheImpl($ cache);

$ evm = new Doctrine\Common\EventManager();
$ em = Doctrine\ORM\EntityManager :: create($ options ['db'],$ config,$ evm);

return $ em;
}
}

现在,您的应用程序可以使用doctrine 2实体管理器。在您的控制器中,您可以这样抓住它:

  $ bootstrap = $ this-> getInvokeArg('bootstrap'); 
$ em = $ bootstrap-> getResource('entitymanager');

我相信这会帮助某人:)


if i do integrate Zend Framework 1.10 with Doctrine 2 where do i put my Doctrine Models/Entities and Proxies? i thought of the /application or the /library directories. if i do put in the /library directory tho, will it interfere with ZF autoloading classes from there since the classes there will be using PHP 5.3 namespaces vs PEAR style namespaces.

解决方案

I'm working on an application that integrates Doctrine 2 with ZF1.10 also.You don't need to use the Doctrine auto loader at all.

1) In your application.ini file add the following line (assuming you have Doctrine installed in your library folder (same as the Zend folder):

autoloadernamespaces.doctrine = "Doctrine"

2) Create a doctrine or entitymanager resource. In your ini file:

resources.entitymanager.db.driver = "pdo_mysql"
resources.entitymanager.db.user = "user"
resources.entitymanager.db.dbname = "db"
resources.entitymanager.db.host = "localhost"
resources.entitymanager.db.password = "pass"
resources.entitymanager.query.cache = "Doctrine\Common\Cache\ApcCache"
resources.entitymanager.metadata.cache = "Doctrine\Common\Cache\ApcCache"
resources.entitymanager.metadata.driver = "Doctrine\ORM\Mapping\Driver\AnnotationDriver"
resources.entitymanager.metadata.proxyDir = APPLICATION_PATH "/../data/proxies"
resources.entitymanager.metadata.entityDir[] = APPLICATION_PATH "/models/entity"

3) Next, you will need to bootstrap it. I added a resource class in my resources folder. Make sure you map to the folder in your ini file:

pluginPaths.Application_Resource_ = APPLICATION_PATH "/resources"

Then your resource class...

class Application_Resource_EntityManager 
extends Zend_Application_Resource_ResourceAbstract
{
    /**
     * @var Doctrine\ORM\EntityManager
     */
    protected $_em;

    public function init()
    {
        $this->_em = $this->getEntityManager();
        return $this->_em;
    }

    public function getEntityManager()
    {
        $options = $this->getOptions(); 
        $config = new \Doctrine\ORM\Configuration();
        $config->setProxyDir($options['metadata']['proxyDir']);
        $config->setProxyNamespace('Proxy');
        $config->setAutoGenerateProxyClasses((APPLICATION_ENV == 'development'));
        $driverImpl = $config->newDefaultAnnotationDriver($options['metadata']['entityDir']);
        $config->setMetadataDriverImpl($driverImpl);
        $cache = new Doctrine\Common\Cache\ArrayCache();
        $config->setMetadataCacheImpl($cache);
        $config->setQueryCacheImpl($cache);

        $evm = new Doctrine\Common\EventManager();
        $em = Doctrine\ORM\EntityManager::create($options['db'],$config,$evm);

        return $em;
    }
}

The doctrine 2 entity manager is now available to your application. In your controller you can grab it like so:

$bootstrap = $this->getInvokeArg('bootstrap');
$em = $bootstrap->getResource('entitymanager');

I am sure this will help somebody :)

这篇关于整合ZF / Doctrine2:我将模型/实体和代理类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆