【问题标题】:Symfony 4 Custom annotation problem @ORM\Entity does not existSymfony 4 自定义注解问题@ORM\Entity 不存在
【发布时间】:2019-11-06 14:49:23
【问题描述】:

作为我在一段时间内发布的 CMS 开发的一部分。我遇到了一个问题。

错误:

[语义错误] ScyLabs\GiftCodeBundle\Entity\GiftCode 类中的注释“@Doctrine\ORM\Mapping\Entity”不存在,或无法自动加载。

我给你解释一下,

基本上,在项目中,一切都是Overridable,已经是这样了,在services.yaml文件中有配置。

出于简单性和迫切需要的明显原因,允许我创建第二个继承自它的包。我告诉自己,做我的“覆盖”或对项目说:“你好,我在这里,我是一个班级使用我”,使用注释非常方便(而且更清晰)。

所以,我创建了一个自定义注释(到目前为止一切都很好..)你可以在这里找到..

<?php
/**
 * Created by PhpStorm.
 * User: alex
 * Date: 04/11/2019
 * Time: 14:25
 */

namespace ScyLabs\NeptuneBundle\Annotation\ScyLabsNeptune;


/**
 * @Annotation
 * @Target({"CLASS"})
 * @Attributes({
 *  @Attribute("key",type="string"),
 *  @Attribute("classNameSpace",type="string"),
 * })
 */
class Override
{
    /**
     * @var string
     */
    public $key;
    /**
     * @var string
     */
    public $classNameSpace;

    public function __construct(array $opts) {

        $this->key = $opts['value'];
        $this->classNameSpace = $opts['class'];

    }
}

好吧,我的注释已经到位,我现在将它放在一个实体中,.. 就像这里

<?php
/**
 * Created by PhpStorm.
 * User: alex
 * Date: 05/11/2019
 * Time: 10:20
 */

namespace ScyLabs\GiftCodeBundle\Entity;

use ScyLabs\NeptuneBundle\Annotation\ScyLabsNeptune;
use Doctrine\ORM\Mapping as ORM;

/**
 *  @ScyLabsNeptune\Override("gift",class="ScyLabs\GiftCodeBundle\Entity\GiftCode")
 *  @ORM\Entity()
 */
class GiftCode
{

}

为什么要这样做?事实上,海王星中的一切都是自动化的,除了特殊情况,它会自动生成实体正常运行所需的所有 URL(添加/编辑/删除/列表)......为此,它必须指出该实体存在的项目,并且它必须是该系统的一部分。

所以,到现在为止我在services.yaml中使用了一个非常完整的配置,我在里面填写了一个表keys => value,对应“key”=>“Namespace”

在我的例子中:“gift”=>“ScyLabs\GiftCodeBundle\Entity\GiftCode”

简而言之,突然,为了覆盖,我在编译步骤中做了一个处理

<?php
/**
 * Created by PhpStorm.
 * User: alex
 * Date: 01/08/2018
 * Time: 09:46
 */

namespace ScyLabs\NeptuneBundle;


class ScyLabsNeptuneBundle extends Bundle
{
    public function getContainerExtension()
    {

// Compilation de l'extension
        return new ScyLabsNeptuneExtension();
    }
}

在这个扩展中,我有这段代码可以完成一切

$bundles = require $projectDir.'/config/bundles.php';

        foreach ($bundles as $bundle => $env){

            if($bundle === ScyLabsNeptuneBundle::class)
                continue;
            if(method_exists(new $bundle,'getParent') && (new $bundle)->getParent() === ScyLabsNeptuneBundle::class){


                $reader = new AnnotationReader();

                $reflClass = new \ReflectionClass(GiftCode::class);

                $classAnotations = $reader->getClassAnnotation($reflClass,"Override");

                foreach ($classAnotations as $classAnotation){
                    if($classAnotation instanceof Override && class_exists($classAnotation->classNameSpace)){
                        $config['override'][$classAnotation->key] = $classAnotation->classNameSpace;                    }
                }
            }
        }

根据我经过大量研究后的怀疑,在我的扩展的编译阶段,@ORM\Entity,和//Autowire,它似乎还没有编译。

问题是,突然,当我得到我的个人注释(覆盖)时,我无法恢复@ORM \ Entity,我不一定能将其删除,因为它不再作为一个实体工作。

为什么要在这里?因为后面我还有一步comoilation(A CompilationPass)

$container->addCompilerPass(new ResolveDoctrineTargetEntitiesPass(),PassConfig::TYPE_BEFORE_OPTIMIZATION,1000);

谁,根据我寄给他的画重新定义了教义将调用的实体(你知道,我之前定义的那个)。

有了这个,我提供了覆盖具有相同名称的实体的可能性。

怎么办?? ..我承认我不能做更多...

提前谢谢朋友们;)

【问题讨论】:

    标签: php symfony annotations doctrine symfony-4.3


    【解决方案1】:

    默认情况下,注释阅读器不使用与类相同的自动加载器。 你需要告诉他如何像这样加载注释类:

    AnnotationRegistry::registerUniqueLoader('class_exists');
    

    更多解释可以看文档https://www.doctrine-project.org/projects/doctrine-annotations/en/1.6/annotations.html#registering-annotations

    【讨论】:

      【解决方案2】:

      感谢您的回复。

      但它不起作用,并且此功能已被弃用并已删除到 Annotations 2.0。

      但是,当我尝试时,我找到了解决方案。

      当我点击您的链接并尝试页面中的代码时,我会尝试此功能

      AnnotationRegistry#registerFile($file)
      

      为了获取@ORM\Entity 文件路径,我使用

      new \ReflectionClass(ORM\Entity::class);
      

      而且,这项工作。 我删除了 AnnotationRegistry#registerFile($file) function ,这个工作。

      感谢您的帮助;) 你是最棒的

      【讨论】:

        猜你喜欢
        • 2022-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多