php 导入注释时出现问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11910147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trouble with importing annotations
提问by Steven Mercatante
I'm working on a CodeIgniter project in which I'm using Doctrine2 and the Symfony2 Validator component.
我正在处理一个 CodeIgniter 项目,在该项目中我使用了 Doctrine2 和 Symfony2 Validator 组件。
All my Doctrine2 entities use Doctrine\ORM\Mappingand the entity manager recognizes them. My entity annotation looks like this:
我所有的 Doctrine2 实体use Doctrine\ORM\Mapping和实体管理器都能识别它们。我的实体注释如下所示:
/**
* @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 Userobject, I get this error:
在这一点上,我能够毫无困难地持久化实体。当我尝试使用 Symfony2 Validator 组件时,出现了第一个问题。当我尝试验证User对象时,出现此错误:
[Semantical Error] The annotation "@Entity" in class Entity\User was never imported. Did you maybe forget to add a "use" statement for this annotation?
[语义错误] 从未导入类 Entity\User 中的注释“@Entity”。您是否忘记为此注释添加“use”语句?
The only "fix" to this issue is through use Doctrine\Mapping\Entity, but I have to do that for everyannotation being used by my entity (Table, Column, ManyToOne, etc.). I'm trying to figure out why I need to explicitely useeach annotation class instead of them being automatically recognized (shouldn't use Doctrine\ORM\Mappinggrant me access to all the classes within that namespace?).
此问题的唯一“修复”是通过use Doctrine\Mapping\Entity,但我必须为我的实体(表、列、ManyToOne 等)使用的每个注释都这样做。我试图弄清楚为什么我需要明确地显示use每个注释类而不是自动识别它们(不应该use Doctrine\ORM\Mapping授予我对该命名空间内所有类的访问权限?)。
So, I then tried use Doctrine\ORM\Mapping as ORMand 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 Userentity is not recognized.
因此,我随后尝试use Doctrine\ORM\Mapping as ORM使用ORM\. 例如:@ORM\Entity()。Symfony2 验证器停止抱怨,但现在 Doctrine2 抱怨Class Entity\User is not a valid entity or mapped super class.我不知道为什么这种方法适用于验证器,而不适用于 Doctrine2。如果我运行控制台命令doctrine:orm:info,User则无法识别我的实体。
Because this is a CodeIgniter app, I'm autoloading the Symfony2 and Doctrine2 libraries. My autoload code is as follows:
因为这是一个 CodeIgniter 应用程序,所以我正在自动加载 Symfony2 和 Doctrine2 库。我的自动加载代码如下:
# 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?
我不在乎是否必须为所有内容做序ORM\,我只想找到一个 Symfony2 Validator 和 Doctrine2 都可以使用的解决方案。有任何想法吗?
回答by user1825294
I had a similar issue when using Silex, Doctrine2 and the Symfony-Validator.
我在使用 Silex、Doctrine2 和 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).
解决方案是避免使用 SimpleAnnotationsReader 而是使用普通的 AnnotationReader (查看Doctrine\ORM\Configuration::newDefaultAnnotationDriver)。然后,您可以在每个实体前加上ORM\(当然也可以use Doctrine\ORM\Mapping as ORM;在每个实体中插入)。
回答by ajtrichards
I resolved this issue by adding the following to my entity file:
我通过将以下内容添加到我的实体文件中解决了这个问题:
use Doctrine\ORM\Mapping as ORM;
So my file now looks like:
所以我的文件现在看起来像:
<?php
namespace Acme\CustomBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Example
*
* @ORM\Entity
* @ORM\Table(name="example")
*
*/
class Example
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*
* @var integer
*/
private $id;
/**
* @ORM\Column(type="string", length=125)
*
* @var string
*/
private $title;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* @param string $title
*
* @return Example
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function gettitle()
{
return $this->title;
}
}
回答by Mostafa Lavaei
Add usestatements for
every annotation you have used in your entities. for example:
添加use报表每次都在使用的标注entities。例如:
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Table;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\GeneratedValue;
But you may not interesting to use this method. You can write all usestatements to one file and just include it in your entities.
但是你可能对使用这种方法不感兴趣。您可以将所有use语句写入一个文件,然后将其包含在entities.
回答by Luis Lopes
If you are testing and your tests fail because of this. Make sure you have the right autoload configured in your phpunit settings.
如果您正在测试并且您的测试因此而失败。确保在 phpunit 设置中配置了正确的自动加载。
For a very specific scenario, my problem was upgrading my symfony version whereas before i had the bootstrap.cache.phpfile, but on a new version, it is app/autoload. I had to change my phpunit.xmlconfig file and set my bootstrap option to that file bootstrap="autoload.php"
对于一个非常具体的场景,我的问题是升级我的 symfony 版本,而在我拥有该bootstrap.cache.php文件之前,但在新版本上,它是app/autoload. 我不得不更改我的phpunit.xml配置文件并将我的引导选项设置为该文件bootstrap="autoload.php"
回答by Ales
I was getting the same error message "[Semantical Error] The annotation "@Entity" in class Entity\User was never imported. Did you maybe forget to add a "use" statement for this annotation?".
我收到相同的错误消息“[语义错误] 类 Entity\User 中的注释“@Entity”从未被导入。您是否忘记为此注释添加“use”语句?”。
The problem was that I had alias for ORM and did not used it for the annotation.
问题是我有 ORM 的别名并且没有将它用于注释。
I had this: @OneToOne(targetEntity="CompanyProduct")
我有这个: @OneToOne(targetEntity="CompanyProduct")
And correct was this: @ORM\OneToOne(targetEntity="CompanyProduct")
正确的是这样的: @ORM\OneToOne(targetEntity="CompanyProduct")
回答by Mostafa
My problem solved by adding:
我的问题通过添加解决:
use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
use Doctrine\ORM\Mapping\HasLifecycleCallbacks;

