2012-02-19 49 views
6

我正在使用Zend框架來創建一個Web應用程序。根據幾項建議,我選擇了Doctrine作爲我的RDBM系統。如何在Doctrine中不使用CLI手動生成代理?

;--------------------------------------------------- 
; DOCTRINE CONFIGURATION 
;--------------------------------------------------- 
resources.entityManager.connection.driver = "pdo_mysql" 
resources.entityManager.connection.host = "localhost" 
resources.entityManager.connection.dbname = "private" 
resources.entityManager.connection.user = "private" 
resources.entityManager.connection.password = "private" 
resources.entityManager.connection.entities = APPLICATION_PATH "/models" 
resources.entityManager.connection.proxies.location = APPLICATION_PATH "/models/Proxies" 
resources.entityManager.connection.proxies.ns = "Proxies" 

; According to Doctrine manual, this should be true for 
; development, and false for production 
resources.entityManager.connection.proxies.generate = true 

以上是我在Zend application.ini中的Doctrine配置。一切工作正常,但我想事先知道如何手動生成代理,而不使用CLI有幾個原因。首先,Doctrine 2.0文檔提到自動生成代理會導致性能問題。其次,我還沒有想出如何使用Doctrine CLI,特別是我已經將我的項目開發移動到共享服務器框而沒有命令提示符訪問。

我一直在通過創建類來手動生成Doctrine實體。我該如何手動生成Doctrine代理?

回答

4

我找到簡單的方法來生成代理:

$proxyDir = null; //to genearate to default proxy dir 
    $proxyFactory = $em->getProxyFactory(); 
    $metadatas = $em->getMetadataFactory()->getAllMetadata(); 
    $proxyFactory->generateProxyClasses($metadatas, $proxyDir); 

生成實體使用:

$classes = $em->getClassMetadataFactory()->getAllMetadata(); 
    $generator = new \Doctrine\ORM\Tools\EntityGenerator(); 
    $generator->setGenerateAnnotations(true); 
    $generator->setGenerateStubMethods(true); 
    $generator->setRegenerateEntityIfExists(false); 
    $generator->setUpdateEntityIfExists(true); 
    $generator->generate($classes, '/path/to/generate/entities'); 
+0

我們有實體,但是你的代碼($ metadatas線)的第三行正在給我們提供錯誤:Uncaught exception'Doctrine \ ORM \ Mapping \ MappingException'帶有消息'文件映射驅動程序必須有一個有效的目錄路徑,但給定的路徑似乎不正確!在C:\ Zend \ Apache2 \ htdocs \ Webate \ library \ Doctrine \ ORM \ Mapping \ MappingException.php:193 – Furyvore 2013-01-27 23:56:18

+0

你需要在EntityManager中設置代理路徑,這裏是我的舊項目代碼:'$ config = new \ Doctrine \ ORM \配置; \t \t $ config-> setMetadataCacheImpl(self :: getCache()); \t \t $ config-> setMetadataDriverImpl(new \ qweb \ driver \ DoctrineYml(QWEB_MODULE)); \t \t $ config-> setEntityNamespaces(array('base')); \t \t $ config-> setProxyDir(DOCTRINE_PROXIES); \t \t $ config-> setProxyNamespace('proxy'); (self :: loadYml(QWEB_CONFIG.DIRECTORY_SEPARATOR.'databases.yml'));}; \t \t $ em = \ Doctrine \ ORM \ EntityManager :: create($ connection,$ config);' – 2013-02-13 17:09:58