简体   繁体   中英

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.

I expect it to create an entity in my bundle Acme\Bundle\TestBundle which I set in my config_dev.yml by using:

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 .

I add the config file in config/packages/dev/maker.yaml

maker:
    root_namespace: 'App\Core'

To generate the entity in the src/Core folder, I got the following error:

➜ 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

Step 1: install package

composer require cweagans/composer-patches

Step 2: apply the patch in 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

composer update

Step 4: try make:entity with the additional command option

➜ 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

These steps worked for me with:

  • "doctrine/doctrine-bundle": "^2.7"
  • PHP 8.1 (I migrate from php 7.4 in my case)

STEP 1: replace type: annotation by type: attribute inside doctrine.yaml

STEP 2: Enter entity root with namespace:

symfony console make:entity  App\Entity\test

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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