【问题标题】:Custom annotation was never imported从未导入自定义注释
【发布时间】:2016-05-12 09:28:31
【问题描述】:

我创建了一个教义注释

namespace Fondative\GenBundle\Front\Annotation;
use Doctrine\Common\Annotations\Annotation;

/**
* @Annotation
* @Target("PROPERTY")
*/
class ReferenceAnnotation extends Annotation {

}

use Fondative\GenBundle\Front\Annotation\ReferenceAnnotation ;

/**
 * A Driver.
 * 
 *
 * 
 * @ORM\Entity
 * 
 */
class Driver {
    /*
     * @Reference
     */
    private $name;

我得到了这个异常

[语义错误] 属性中的注释“@Reference” Fondative\TestBundle\Entity\Driver::$name 从未被导入。

【问题讨论】:

  • 试试这个使用声明:use Fondative\GenBundle\Front\Annotation\ReferenceAnnotation as Reference;
  • 我调用了错误的注释名称:

标签: doctrine-orm symfony


【解决方案1】:

一个非常常见的错误;)

Doctrine/Annotation从cmetsviaReflectionProperty::getDocComment()读取注解。

问题示例:

class MyClassA
{
    /**
     * Foo
     */
    private $foo;

    /*
     * Bar
     */
    private $bar;
}


$ref = new \ReflectionClass('MyClassA');

print sprintf(
    "Comment of MyClassA::foo -> \n%s\n\n",
    $ref->getProperty('foo')->getDocComment()
);

print sprintf(
    "Comment of MyClassA::bar -> \n%s\n\n",
    $ref->getProperty('bar')->getDocComment()
);

属性foo 有一个doc cmets,但属性bar 没有注释,因为声明注释中存在拼写错误(一个特殊字符*)。

在 PHP doc cmets 中必须从两个字符开始 *!

修正这个错字,一切正常 ;)

【讨论】:

    【解决方案2】:
    class Driver {
        /*
         * @ReferenceAnnotation
         */
        private $name;
    

    或者将注解类重命名为Reference和

    use Fondative\GenBundle\Front\Annotation\Reference as Reference ;
         /*
         * @Reference
         */
        private $name;
    

    【讨论】:

      猜你喜欢
      • 2019-07-26
      • 2016-11-04
      • 1970-01-01
      • 1970-01-01
      • 2016-06-08
      • 2019-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多