dtpd58676 2010-07-24 12:23
浏览 46
已采纳

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

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.

  • 写回答

3条回答 默认 最新

  • douxueke5653 2010-08-27 01:07
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路