2012-04-22 13 views
6

エンティティファイルの注釈レジストリにアクセスするには、\Doctrine\Common\Annotations\AnnotationRegistry::registerFileを使用する必要があります。clctを持つdoctrine2 autloaderはAnnotationRegistryを使用する必要があります

この部分は、ドライバチェーンを使用し、orm:schema-tool:creatorを使用するために必要です。しかし、私はAnnotationRegistry::registerFileを追加して必要な各クラスを追加できません。

私のDoctrine 2.2.2にGedmoを追加したいときに、この問題が発生しました。

// 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の注釈を読み、スプラインオートローディングでジョブを実行できない場合は、ソリューションを使用する必要があります。

+0

_Symfony 2コンソールアプリケーション_を開発している場合は、このスニペットを[この回答で説明されている方法](http://stackoverflow.com/a/13461155)に置き換えてください:http://stackoverflow.com/a/ 13461155 – xsubira

-2

ありがとうSamuel Herzog。それは私のために完璧に動作します。私は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

コメントから明らかなように、これは生成されたファイルです。あなたの編集は、次の 'composer install/update'で上書きされます。 –

関連する問題