0

作为我一段时间后发布的 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);

谁,重新定义了学说将调用的与我发送给他的画相关的实体(你知道,我之前定义的那个)。

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

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

在此先感谢朋友;)

4

2 回答 2

0

谢谢你的回复。

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

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

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

AnnotationRegistry#registerFile($file)

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

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

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

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

于 2019-11-07T10:25:42.617 回答
0

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

AnnotationRegistry::registerUniqueLoader('class_exists');

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

于 2019-11-06T18:47:18.633 回答