我正在开发一个 CodeIgniter 项目,在该项目中我使用了 Doctrine2 和 Symfony2 Validator 组件.
I'm working on a CodeIgniter project in which I'm using Doctrine2 and the Symfony2 Validator component.
我所有的 Doctrine2 实体 使用 DoctrineORMMapping
并且实体管理器识别它们.我的实体注释如下所示:
All my Doctrine2 entities use DoctrineORMMapping
and the entity manager recognizes them. My entity annotation looks like this:
/**
* @Entity(repositoryClass = "UserRepository")
* @Table(name = "user")
* @HasLifecycleCallbacks()
*/
此时,我可以毫无困难地持久化实体.第一个问题出现在我尝试使用 Symfony2 Validator 组件时.当我尝试验证 User
对象时,出现此错误:
At this point, I'm able to persist entities without any trouble. The first problem arises when I try to use the Symfony2 Validator component. When I try to validate a User
object, I get this error:
[语义错误] EntityUser 类中的注释@Entity"从未被导入.您是否忘记为此注释添加use"语句?
[Semantical Error] The annotation "@Entity" in class EntityUser was never imported. Did you maybe forget to add a "use" statement for this annotation?
这个问题的唯一修复"是通过use DoctrineMappingEntity
,但我必须为我的实体使用的每个注释(表、列、多对一等).我试图弄清楚为什么我需要明确地use
每个注释类而不是它们被自动识别(不应该 use DoctrineORMMapping
授予我访问该命名空间中的所有类?).
The only "fix" to this issue is through use DoctrineMappingEntity
, but I have to do that for every annotation being used by my entity (Table, Column, ManyToOne, etc.). I'm trying to figure out why I need to explicitely use
each annotation class instead of them being automatically recognized (shouldn't use DoctrineORMMapping
grant me access to all the classes within that namespace?).
因此,我尝试了使用 DoctrineORMMapping 作为 ORM
并使用 ORM
在我的所有注释前加上.例如:@ORMEntity()
.Symfony2 验证器停止抱怨,但现在 Doctrine2 抱怨 Class EntityUser 不是有效实体或映射的超类.
我不知道为什么这种方法适用于验证器,而不适用于 Doctrine2.如果我运行控制台命令 doctrine:orm:info
,则无法识别我的 User
实体.
So, I then tried use DoctrineORMMapping as ORM
and prefacing all my annotations with ORM
. Ex: @ORMEntity()
. The Symfony2 validator stops complaining, but now Doctrine2 complains that Class EntityUser is not a valid entity or mapped super class.
I have no idea why this method works for the Validator, but not Doctrine2. If I run the console command doctrine:orm:info
, my User
entity is not recognized.
因为这是一个 CodeIgniter 应用程序,所以我正在自动加载 Symfony2 和 Doctrine2 库.我的自动加载代码如下:
Because this is a CodeIgniter app, I'm autoloading the Symfony2 and Doctrine2 libraries. My autoload code is as follows:
# Symfony2 ClassLoader component
require_once __DIR__ . '/application/libraries/symfony/src/Symfony/Component/ClassLoader/UniversalClassLoader.php';
$loader = new SymfonyComponentClassLoaderUniversalClassLoader();
$loader->register();
$loader->registerNamespace('Symfony', __DIR__ . '/application/libraries/symfony/src');
$loader->registerNamespace('Doctrine', __DIR__ . '/application/libraries/doctrine/lib');
# Doctrine2
require_once __DIR__ . '/application/libraries/doctrine/lib/Doctrine/Common/Annotations/AnnotationRegistry.php';
use DoctrineCommonAnnotationsAnnotationRegistry;
AnnotationRegistry::registerLoader(function($class) use ($loader) {
$loader->loadClass($class);
$classExists = class_exists($class, false);
return $classExists;
});
AnnotationRegistry::registerFile(__DIR__ . '/application/libraries/doctrine/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php');
我不在乎是否必须以 ORM
开头所有内容,我只想找到一个 Symfony2 Validator 和 Doctrine2 都可以使用的解决方案.有什么想法吗?
I don't care if I have to preface everything with ORM
or not, I just want to find a solution that the Symfony2 Validator and Doctrine2 will both work with. Any ideas?
我在使用 Silex、Doctrine2 和 Symfony-Validator 时遇到了类似的问题.
I had a similar issue when using Silex, Doctrine2 and the Symfony-Validator.
解决方案是避免使用 SimpleAnnotationsReader 而是使用普通的 AnnotationReader(查看 DoctrineORMConfiguration::newDefaultAnnotationDriver).然后你可以用 ORM
为每个实体做序(当然,在每个实体中插入 use DoctrineORMMapping as ORM;
).
The solution was to avoid using the SimpleAnnotationsReader and instead using the normal AnnotationReader (have a look at DoctrineORMConfiguration::newDefaultAnnotationDriver). You can then preface every entity with ORM
(and of course inserting use DoctrineORMMapping as ORM;
in every entity).
这篇关于导入注释时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!