douqipi9704 2012-08-10 22:40
浏览 316
已采纳

导入注释时遇到问题

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?

  • 写回答

6条回答 默认 最新

  • dporu02280 2012-11-15 00:06
    关注

    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).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 爬取1-112页所有帖子的标题但是12页后要登录后才能 我使用selenium模拟登录 账号密码输入后 会报错 不知道怎么弄了
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点