Symfony 4自定义注释问题@ORM \ Entity不存在

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

错误:

  

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

我向你解释,

基本上,在项目中,所有内容都是可重写的,并且已经在文件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",* })
 */
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(ADD / EDIT / DELETE / LIST)...为此,它必须指示实体存在的项目,并且该实体必须是该系统的一部分。

因此,到目前为止,我在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,也不一定要删除它,因为它不能再作为实体使用。

为什么在这里呢?因为在后面,我还有另一步整理(CompilationPass)

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

谁重新定义了与我发送给他的绘画有关的学说的实体(您知道,我刚刚定义的那个实体)。

通过这种方式,我可以覆盖具有相同名称的实体。

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

在此先感谢朋友;)

cecilia53520 回答:Symfony 4自定义注释问题@ORM \ Entity不存在

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

AnnotationRegistry::registerUniqueLoader('class_exists');

有关更多说明,您可以查看文档https://www.doctrine-project.org/projects/doctrine-annotations/en/1.6/annotations.html#registering-annotations

,

感谢您的回复。

但是它不起作用,此功能已弃用,并已从注释2.0中删除。

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

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

AnnotationRegistry#registerFile($file)

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

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

然后,这项工作。 我删除了AnnotationRegistry#registerFile($file)函数,这项工作。

谢谢您的帮助;) 你是最好的

本文链接:https://www.f2er.com/3151228.html

大家都在问