dongyishen5796 2011-11-23 00:57
浏览 65

如何在Symfony2之外启用ORM注释前缀?

I'm converting an old PHP project to the Symfony2 framework. Some of the pages are now handled by my Symfony2 front controller (index.php), but many pages have not yet been converted.

The problem is that, within Symfony, all of my Doctrine entity annotations must begin with the ORM\ prefix, but outside of Symfony, that prefix does not appear to be enabled, and so I get the following error:

Class MyProject\MyBundle\Entity\MyClass is not a valid entity or mapped super class.

I've tried to duplicate whatever magic Symfony does to set this up, including following these instructions [doctrine-project.org], and actually including app/autoload.php entirely into my legacy bootstrap process. But nothing works.

Does anyone know how I can manually replicate whatever it is that Symfony does to enable the ORM\ prefix for my Doctrine annotations?

  • 写回答

2条回答 默认 最新

  • dph23577 2011-11-23 20:42
    关注

    I got the answer from the Symfony2 Google group. The problem is that the Doctrine configuration shown in the documentation uses SimpleAnnotationReader behind the scenes, but you need regular AnnotationReader to use the ORM\ namespace prefix. I got it to work by replacing this:

    $config = new Doctrine\ORM\Configuration();
    $driver = $config->newDefaultAnnotationDriver('/path/to/my/entities');
    

    with this:

    use Doctrine\Common\Annotations\AnnotationReader;
    use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
    // ...
    $config = new Doctrine\ORM\Configuration();
    $reader = new AnnotationReader();
    $driver = new AnnotationDriver($reader, '/path/to/my/entities');
    
    评论

报告相同问题?