doctrine2 autloader with cli must use AnnotationRegistry

倾然丶 夕夏残阳落幕 提交于 2019-12-03 13:33:51

问题


I must use \Doctrine\Common\Annotations\AnnotationRegistry::registerFile to access the annotation registry in entity files.

This part is required to use driver chain and using orm:schema-tool:creator. but i cant add each class i needed by adding AnnotationRegistry::registerFile.

this problem was see when i want to add Gedmo to my 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;

and my entity file it's here

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

the error is :

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

回答1:


It seems your AnnotationRegistry isn't set up.

Add the following to your script:

use Doctrine\Common\Annotations\AnnotationRegistry;  

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

see Doctrine manual on Annotations for the detailed explanation.
In short: to read the annotations PSR-0 and spl autoloading can't do the job, you have to use their solution.




回答2:


Thank you Samuel Herzog. It works perfect for me. I was generating the Vendors using composer.json. So When I added my autoload.php i just needed to add these sentences...

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

Just like that.



来源:https://stackoverflow.com/questions/10269733/doctrine2-autloader-with-cli-must-use-annotationregistry

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!