簡體   English   中英

Symfony make:entity 注解映射錯誤

[英]Symfony make:entity annotation mapping error

我想使用 MakerBundle 中的 make:entity 命令在我的 ORO 平台應用程序中創建一個新實體。

我希望它在我的包Acme\Bundle\TestBundle中創建一個實體,我使用以下方法在config_dev.yml中設置它:

maker:
    root_namespace: 'Acme\Bundle\TestBundle'

所以我執行

bin/console make:entity Test

返回

 ! [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.                                                   

我嘗試再次運行該命令,該命令有效。 但顯然這不是它的工作方式。 那么如何解決這個映射錯誤呢?

我通過使用Symfony new myapp開始了標准的 Symfony 5 項目。

我在config/packages/dev/maker.yaml添加配置文件

maker:
    root_namespace: 'App\Core'

要在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.

為了避免在控制台中顯示錯誤,我安裝了由vklux / maker-bundle-force-annotation創建的補丁

第一步:安裝包

composer require cweagans/composer-patches

第 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"
            }
        }
    }
}

第 3 步:作曲家更新

composer update

第 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):
 >

✅ 🚀 👍 現在一切正常。

這是教義版本中的一個錯誤,請試試這個版本祝你好運

composer req doctrine/doctrine-bundle:2.2

我找到了使用高於2.2.4的教義/教義捆綁版本的 Symfony 制造商創建新實體的解決方案

這些步驟對我有用:

  • "doctrine/doctrine-bundle": "^2.7"
  • PHP 8.1(在我的情況下,我從 php 7.4 遷移)

第 1 步:替換type: annotation type: attribute doctrine.yaml內部的屬性

第 2步:使用命名空間輸入實體根目錄:

symfony console make:entity  App\Entity\test

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM