集成 ZF/Doctrine2:我在哪里放置我的模型/实体和代理类

2022-01-16 00:00:00 namespaces php doctrine zend-framework

如果我确实将 Zend Framework 1.10 与 Doctrine 2 集成在一起,我将我的 Doctrine 模型/实体和代理放在哪里?我想到了 /application/library 目录.如果我确实放入 /library 目录,它会干扰 ZF 从那里自动加载类,因为那里的类将使用 PHP 5.3 命名空间与 PEAR 样式命名空间.

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.

推荐答案

我正在开发一个集成 Doctrine 2 和 ZF1.10 的应用程序.你根本不需要使用 Doctrine 自动加载器.

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) 在您的 application.ini 文件中添加以下行(假设您在库文件夹中安装了 Doctrine(与 Zend 文件夹相同):

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) 创建一个学说或实体管理器资源.在您的 ini 文件中:

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 = "DoctrineCommonCacheApcCache"
resources.entitymanager.metadata.cache = "DoctrineCommonCacheApcCache"
resources.entitymanager.metadata.driver = "DoctrineORMMappingDriverAnnotationDriver"
resources.entitymanager.metadata.proxyDir = APPLICATION_PATH "/../data/proxies"
resources.entitymanager.metadata.entityDir[] = APPLICATION_PATH "/models/entity"

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

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"

那么你的资源类...

class Application_Resource_EntityManager 
extends Zend_Application_Resource_ResourceAbstract
{
    /**
     * @var DoctrineORMEntityManager
     */
    protected $_em;

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

    public function getEntityManager()
    {
        $options = $this->getOptions(); 
        $config = new DoctrineORMConfiguration();
        $config->setProxyDir($options['metadata']['proxyDir']);
        $config->setProxyNamespace('Proxy');
        $config->setAutoGenerateProxyClasses((APPLICATION_ENV == 'development'));
        $driverImpl = $config->newDefaultAnnotationDriver($options['metadata']['entityDir']);
        $config->setMetadataDriverImpl($driverImpl);
        $cache = new DoctrineCommonCacheArrayCache();
        $config->setMetadataCacheImpl($cache);
        $config->setQueryCacheImpl($cache);

        $evm = new DoctrineCommonEventManager();
        $em = DoctrineORMEntityManager::create($options['db'],$config,$evm);

        return $em;
    }
}

现在您的应用程序可以使用原则 2 实体管理器.在您的控制器中,您可以像这样抓取它:

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 :)

相关文章