导入注解的麻烦 [英] Trouble with importing annotations
问题描述
我所有的Doctrine2实体使用Doctrine\ORM\Mapping
,实体管理员可以识别它们。我的实体注释如下所示:
/ **
* @Entity(repositoryClass =UserRepository $ b * @Table(name =user)
* @HasLifecycleCallbacks()
* /
在这一点上,我能够坚持实体没有任何麻烦。当我尝试使用Symfony2 Validator组件时,会出现第一个问题。当我尝试验证用户
对象时,我收到以下错误:
[语义错误]类Entity\User中的注释@Entity从未导入。你可能忘记为这个注释添加一个使用语句?
这个问题的唯一修复是通过使用Doctrine\Mapping\Entity
,但是我必须为我的实体正在使用的每个注释(Table,Column,ManyToOne等) )。我想弄清楚为什么我需要明确地使用
每个注释类,而不是自动识别(不应该使用Doctrine\ORM \Mapping
允许我访问该命名空间中的所有类?)。
所以,我尝试使用Doctrine\ORM\映射为ORM
,并以 ORM\
开头的所有注释。例如: @ ORM\Entity()
。 Symfony2验证器停止抱怨,但现在Doctrine2抱怨类Entity\User不是有效的实体或映射的超类。我不知道为什么这种方法适用于验证器但不是教义2。如果我运行控制台命令
doctrine:orm:info
,我的用户
实体无法识别。
因为这是一个CodeIgniter应用程序,我正在自动加载Symfony2和Doctrine2库。我的自动加载代码如下:
#Symfony2 ClassLoader组件
require_once __DIR__。 /application/libraries/symfony/src/Symfony/Component/ClassLoader/UniversalClassLoader.php;
$ loader = new \Symfony\Component\ClassLoader\UniversalClassLoader();
$ 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;
使用Doctrine\Common\Annotations\AnnotationRegistry;
AnnotationRegistry :: registerLoader($ 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验证器和Doctrine2都可以使用。任何想法?
当使用Silex,Doctrine2和Symfony-Validator时,我有一个类似的问题。
解决方案是避免使用SimpleAnnotationsReader,而是使用正常的AnnotationReader(请查看 Doctrine\ORM\Configuration :: newDefaultAnnotationDriver )。然后,您可以使用 ORM\
(当然插入使用Doctrine\ORM\Mapping作为ORM,
在每个实体中)。
I'm working on a CodeIgniter project in which I'm using Doctrine2 and the Symfony2 Validator component.
All my Doctrine2 entities use Doctrine\ORM\Mapping
and the entity manager recognizes them. My entity annotation looks like this:
/**
* @Entity(repositoryClass = "UserRepository")
* @Table(name = "user")
* @HasLifecycleCallbacks()
*/
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:
[Semantical Error] The annotation "@Entity" in class Entity\User was never imported. Did you maybe forget to add a "use" statement for this annotation?
The only "fix" to this issue is through use Doctrine\Mapping\Entity
, 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 Doctrine\ORM\Mapping
grant me access to all the classes within that namespace?).
So, I then tried use Doctrine\ORM\Mapping as ORM
and prefacing all my annotations with ORM\
. Ex: @ORM\Entity()
. The Symfony2 validator stops complaining, but now Doctrine2 complains that Class Entity\User 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.
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 \Symfony\Component\ClassLoader\UniversalClassLoader();
$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 Doctrine\Common\Annotations\AnnotationRegistry;
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');
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?
I had a similar issue when using Silex, Doctrine2 and the Symfony-Validator.
The solution was to avoid using the SimpleAnnotationsReader and instead using the normal AnnotationReader (have a look at Doctrine\ORM\Configuration::newDefaultAnnotationDriver). You can then preface every entity with ORM\
(and of course inserting use Doctrine\ORM\Mapping as ORM;
in every entity).
这篇关于导入注解的麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!