带cli的doctrine2自动加载器必须使用AnnotationRegistry

时间:2012-04-22 16:23:37

标签: annotations doctrine-orm command-line-interface autoloader

我必须使用\Doctrine\Common\Annotations\AnnotationRegistry::registerFile来访问实体文件中的注释注册表。

这部分是使用驱动程序链和使用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.

2 个答案:

答案 0 :(得分:9)

似乎您的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自动加载不能完成这项工作,你必须使用他们的解决方案。

答案 1 :(得分:-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;

就像那样。