使用cli的doctrine2 autloader必须使用AnnotationRegistry [英] doctrine2 autloader with cli must use AnnotationRegistry

查看:188
本文介绍了使用cli的doctrine2 autloader必须使用AnnotationRegistry的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

此部分需要使用驱动程序链并使用orm:schema-tool:creator。但是我不能添加每个我需要的类,通过添加 AnnotationRegistry :: registerFile

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.

将Gedmo添加到我的Doctrine 2.2.2。

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;

我的实体文件在这里

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.


推荐答案

似乎你的 AnnotationRegistry 未设置。

将以下内容添加到脚本中:

Add the following to your script:

use Doctrine\Common\Annotations\AnnotationRegistry;  

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

请参阅注释中的原则手册详细说明。

简而言之:阅读注释PSR- 0和spl自动加载不能做这个工作,你必须使用他们的解决方案。

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.

这篇关于使用cli的doctrine2 autloader必须使用AnnotationRegistry的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆