2012-04-22 15 views
6

Devo utilizzare \Doctrine\Common\Annotations\AnnotationRegistry::registerFile per accedere al registro delle annotazioni nei file di entità.autototer doctrine2 con cli deve utilizzare AnnotationRegistry

Questa parte è necessaria per utilizzare la catena di driver e utilizzare orm: schema-tool: creatore. ma non posso aggiungere ogni classe di cui avevo bisogno aggiungendo AnnotationRegistry::registerFile.

questo problema è stato vedere quando voglio aggiungere Gedmo al mio 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; 

e il mio file di un'entità che è qui

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

l'errore è:

[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. 

risposta

8

Sembra che il tuo AnnotationRegistry non sia configurato.

Aggiungere il seguente allo script:

use Doctrine\Common\Annotations\AnnotationRegistry; 

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

vedere Doctrine manual on Annotations per la spiegazione dettagliata.
In breve: per leggere le annotazioni PSR-0 e spl autoloading non possono fare il lavoro, devi usare la loro soluzione.

+0

Se si sta sviluppando una _Symfony 2 Console Application_, si preferisce posizionare questo snippet come spiegato [in questa risposta] (http://stackoverflow.com/a/13461155): http://stackoverflow.com/a/ 13461155 – xsubira

-2

Grazie Samuel Herzog. Funziona perfettamente per me. Stavo generando i venditori usando compositore.json. Quindi quando ho aggiunto il mio autoload.php ho solo bisogno di aggiungere queste frasi ...

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

Proprio così.

+4

Come è ovvio dal commento, questo è un file generato. La tua modifica può/verrà sovrascritta nel prossimo 'compattore install/update'. –

Problemi correlati