2012-04-22 68 views
6

我必須使用\Doctrine\Common\Annotations\AnnotationRegistry::registerFile來訪問實體文件中的註釋註冊表。帶cli的doctrine2自動加載程序必須使用AnnotationRegistry

這部分是使用驅動程序鏈和使用orm:schema-tool:creator所必需的。但我不能添加我需要的每個類加入AnnotationRegistry::registerFile

當我想將Gedmo添加到我的Doctrine 2.2.2中時,看到此問題。

// cli-config.php 
// if comment this like an error will appear 
// \Doctrine\Common\Annotations\AnnotationRegistry::registerFile(__DIR__ . '/../library/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php'); 

// cache 
$cache = new \Doctrine\Common\Cache\ArrayCache(); 

// annotation reader 
$annotationReader = new \Doctrine\Common\Annotations\AnnotationReader(); 

// cached annotation reader 
$cachedAnnotationReader = new \Doctrine\Common\Annotations\CachedReader($annotationReader, $cache); 

// driver chain 
$driverChain = new \Doctrine\ORM\Mapping\Driver\DriverChain(); 

// annotation driver 
$annotationDriver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($cachedAnnotationReader, array(SCHEMA_PATH)); 

// add entity namespaces 
$driverChain->addDriver($annotationDriver, 'Entity'); 

// configuration 
$config = new \Doctrine\ORM\Configuration(); 
$config->setMetadataCacheImpl($cache); 
$config->setMetadataDriverImpl($driverChain); 
$config->setQueryCacheImpl($cache); 
$config->setProxyDir(PROXY_PATH); 
$config->setProxyNamespace('Proxies'); 
$config->setAutoGenerateProxyClasses(true); 

// entity manager 
$entityManager = \Doctrine\ORM\EntityManager::create($connectionOptions, $config); 

// helper set 
$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
      'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($entityManager->getConnection()), 
      'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($entityManager) 
     )); 

// return 
return $helperSet; 

和我的實體文件,它在這裏

namespace Entity; 

use \Doctrine\Common\Collections\ArrayCollection; 
use \Doctrine\ORM\Mapping as ORM; 

/** 
* @ORM\Entity 
* @ORM\Table(name="users") 
*/ 
class User 
{ 
    /** 
    * @ORM\Id 
    * @ORM\Column(columnDefinition="INT unsigned NOT NULL") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 

    /** 
    * @ORM\Column(type="string",length=32) 
    */ 
    protected $name; 

    public function getId() 
    { 
     return $this->id; 
    } 

    public function setId($id) 
    { 
     $this->_id = $id; 
     return $this; 
    } 

    public function getName() 
    { 
     return $this->id; 
    } 

    public function setName($name) 
    { 
     $this->_id = $name; 
     return $this; 
    } 
} 

的錯誤是:

[Doctrine\Common\Annotations\AnnotationException]                      
    [Semantical Error] The annotation "@\Doctrine\ORM\Mapping\Entity" in class Entity\User does not exist, or could not be auto-loaded. 

回答

8

看來你AnnotationRegistry不成立。

以下內容添加到您的腳本:

use Doctrine\Common\Annotations\AnnotationRegistry; 

AnnotationRegistry::registerFile("/path/to/doctrine/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php"); 

看到Doctrine manual on Annotations了詳細的解釋。
總之:要閱讀註釋PSR-0和spl自動加載無法完成這項工作,您必須使用他們的解決方案。

+0

如果你正在開發一個_Symfony 2控制檯Application_,你應該把這個代碼片段放在這個答案中(http://stackoverflow.com/a/13461155):http://stackoverflow.com/a/ 13461155 – xsubira 2016-07-26 20:51:03

-2

謝謝塞繆爾赫爾佐格。它適用於我。我正在使用composer.json生成供應商。所以當我添加我的autoload.php我只需要添加這些句子...

<?php 
// autoload.php generated by Composer 
use Doctrine\Common\Annotations\AnnotationRegistry; 

require_once dirname(__DIR__).'/vendor/composer/autoload_real.php'; 

$loader = ComposerAutoloaderInit0cf45a42473ebbcd4516460f93747271::getLoader(); 

AnnotationRegistry::registerFile(dirname(__DIR__).'/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php'); 

return $loader; 

就是這樣。

+4

從評論中可以看出,這是一個生成的文件。您的編輯可以在下一個「作曲家安裝/更新」中被覆蓋。 – 2013-12-26 10:45:02

相關問題