时间:2010-07-24 12:23:19

标签: php zend-framework namespaces doctrine

3 个答案:

答案 0 :(得分:8)

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

1)在你的application.ini文件中添加以下行(假设你的库文件夹中安装了Doctrine(与Zend文件夹相同):

autoloadernamespaces.doctrine = "Doctrine"

2)创建一个doctrine或entitymanager资源。在您的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"

然后你的资源类......

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;
    }
}

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

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

我相信这会对某人有所帮助:)。

答案 1 :(得分:1)

答案 2 :(得分:0)

相关问题