简体   繁体   English

Symfony make:entity 注解映射错误

[英]Symfony make:entity annotation mapping error

I want to create a new entity in my ORO platform application by using the make:entity command from the MakerBundle.我想使用 MakerBundle 中的 make:entity 命令在我的 ORO 平台应用程序中创建一个新实体。

I expect it to create an entity in my bundle Acme\Bundle\TestBundle which I set in my config_dev.yml by using:我希望它在我的包Acme\Bundle\TestBundle中创建一个实体,我使用以下方法在config_dev.yml中设置它:

maker:
    root_namespace: 'Acme\Bundle\TestBundle'

So I execute所以我执行

bin/console make:entity Test

which returns返回

 ! [NOTE] It looks like your app may be using a namespace other than "Acme\Bundle\TestBundle".                 
 !                                                                                                                      
 !        To configure this and make your life easier, see:                                                             
 !        https://symfony.com/doc/current/bundles/SymfonyMakerBundle/index.html#configuration                           

 created: src/Acme/Bundle/TestBundle/Entity/Test.php
 created: src/Acme/Bundle/TestBundle/Repository/TestRepository.php


 [ERROR] Only annotation mapping is supported by make:entity, but the                                                   
         <info>Acme\Bundle\TestBundle\Entity\Test</info> class uses a different format. If you would like   
         this command to generate the properties & getter/setter methods, add your mapping configuration, and then      
         re-run this command with the <info>--regenerate</info> flag.                                                   

I've tried to run the command once again, which works.我尝试再次运行该命令,该命令有效。 But obviously this is not the way how it's meant to work.但显然这不是它的工作方式。 So how can I fix this mapping error?那么如何解决这个映射错误呢?

I started with the standard Symfony 5 project by using Symfony new myapp .我通过使用Symfony new myapp开始了标准的 Symfony 5 项目。

I add the config file in config/packages/dev/maker.yaml我在config/packages/dev/maker.yaml添加配置文件

maker:
    root_namespace: 'App\Core'

To generate the entity in the src/Core folder, I got the following error:要在src/Core文件夹中生成实体,我收到以下错误:

➜ symfony console make:entity Post

 created: src/Core/Entity/Post.php
 created: src/Core/Repository/PostRepository.php


 [ERROR] Only annotation mapping is supported by make:entity, but the <info>App\Core\Entity\Post</info> class uses
         a different format. If you would like this command to generate the properties & getter/setter methods, add your
         mapping configuration, and then re-run this command with the <info>--regenerate</info> flag.

To avoid showing the error in the console, I install the patch created by vklux / maker-bundle-force-annotation为了避免在控制台中显示错误,我安装了由vklux / maker-bundle-force-annotation创建的补丁

Step 1: install package第一步:安装包

composer require cweagans/composer-patches

Step 2: apply the patch in composer.json第 2 步:在composer.json应用补丁

{
    // {...} composer.json content
    "extra": {
        "patches": {
            "symfony/maker-bundle": {
                "Provide flag to force annotation in make entity command": "https://raw.githubusercontent.com/vklux/maker-bundle-force-annotation/master/maker-force-annotation-flag.patch"
            }
        }
    }
}

Step 3: composer update第 3 步:作曲家更新

composer update

Step 4: try make:entity with the additional command option第 4 步:尝试使用附加命令选项make:entity

➜ symfony console make:entity Post --force-annotation

 created: src/Core/Entity/Post.php
 created: src/Core/Repository/PostRepository.php

 Entity generated! Now let's add some fields!
 You can always add more fields later manually or by re-running this command.

 New property name (press <return> to stop adding fields):
 >

✅ 🚀 👍 Everything works fine now. ✅ 🚀 👍 现在一切正常。

这是教义版本中的一个错误,请试试这个版本祝你好运

composer req doctrine/doctrine-bundle:2.2

I found a solution to create new entities with Symfony maker using doctrine/doctrine-bundle version higher than 2.2.4我找到了使用高于2.2.4的教义/教义捆绑版本的 Symfony 制造商创建新实体的解决方案

These steps worked for me with:这些步骤对我有用:

  • "doctrine/doctrine-bundle": "^2.7"
  • PHP 8.1 (I migrate from php 7.4 in my case) PHP 8.1(在我的情况下,我从 php 7.4 迁移)

STEP 1: replace type: annotation by type: attribute inside doctrine.yaml第 1 步:替换type: annotation type: attribute doctrine.yaml内部的属性

STEP 2: Enter entity root with namespace:第 2步:使用命名空间输入实体根目录:

symfony console make:entity  App\Entity\test

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM