gpt4 book ai didi

annotations - 带有 cli 的 Doctic2 自动加载程序必须使用 AnnotationRegistry

转载 作者:行者123 更新时间:2023-12-04 14:49:06 25 4
gpt4 key购买 nike

我必须使用 \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.

最佳答案

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

关于annotations - 带有 cli 的 Doctic2 自动加载程序必须使用 AnnotationRegistry,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10269733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com