You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Symfony Maker Bundle创建Doctrine实体时遇@ApiResource参数错误

问题:Symfony中使用make:entity创建API Platform实体时报错

在Symfony 5.4项目中执行php bin/console make:entity命令,选择将实体标记为API Platform资源时,立即触发错误,无法继续添加属性。手动安装过doctrine/annotations1.14.3和fig/link-util^1.1,因为这两个依赖未被自动安装。

错误日志

In AnnotationException.php line 53:
[Creation Error] An error occurred while instantiating the annotation @ApiResource declared on class App\Entity\MyEntityName: "Argument 1 passed to ApiPlatform\Metadata\ApiResource::__construct() must be o  
  f the type string or null, array given, called in /MyDirectory/Projects/MyProject/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/DocParser.php on line 1493".  

In ApiResource.php line 172:
                                                                                                                                                                                                                  
  Argument 1 passed to ApiPlatform\Metadata\ApiResource::__construct() must be of the type string or null, array given, called in /MyDirectory/Projects/MyProject/vendor/doctrine/annotatio
  ns/lib/Doctrine/Common/Annotations/DocParser.php on line 1493

项目composer.json内容

{
    "name": "app/projectname",
    "type": "project",
    "license": "MIT",
    "description": "Project Description ",
    "minimum-stability": "stable",
    "prefer-stable": true,
    "require": {
        "php": ">=7.2.5",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "api-platform/core": "^2.7",
        "doctrine/annotations": "1.14.3",
        "doctrine/doctrine-bundle": "2.5",
        "doctrine/doctrine-migrations-bundle": "^3.2",
        "doctrine/orm": "^2.10",
        "fig/link-util": "^1.1",
        "mpdf/mpdf": "^8.1",
        "phpoffice/phpspreadsheet": "^1.28",
        "symfony/asset": "^5.4",
        "symfony/console": "5.4.*",
        "symfony/dotenv": "5.4.*",
        "symfony/expression-language": "^5.4",
        "symfony/flex": "^1.17|^2",
        "symfony/framework-bundle": "5.4.*",
        "symfony/monolog-bundle": "^3.8",
        "symfony/property-access": "^5.4",
        "symfony/property-info": "^5.4",
        "symfony/proxy-manager-bridge": "^5.4",
        "symfony/runtime": "5.4.*",
        "symfony/security-bundle": "^5.4",
        "symfony/serializer": "^5.4",
        "symfony/twig-bundle": "^5.4",
        "symfony/validator": "^5.4",
        "symfony/yaml": "^5.4",
        "tecnickcom/tcpdf": "^6.6"
    },
    "require-dev": {
        "doctrine/doctrine-fixtures-bundle": "^3.4",
        "dompdf/dompdf": "^2.0",
        "mitoteam/jpgraph": "^10.2",
        "phploc/phploc": "^7.0",
        "phpunit/phpunit": "^9.5",
        "squizlabs/php_codesniffer": "^3.7",
        "symfony/browser-kit": "5.4.*",
        "symfony/css-selector": "5.4.*",
        "symfony/debug-bundle": "^5.4",
        "symfony/http-client": "^5.4",
        "symfony/maker-bundle": "^1.43",
        "symfony/phpunit-bridge": "^6.2",
        "symfony/stopwatch": "5.4.*",
        "symfony/var-dumper": "^5.4",
        "symfony/web-profiler-bundle": "^5.4.*"
    },
    "config": {
        "allow-plugins": {
            "composer/package-versions-deprecated": true,
            "php-http/discovery": true,
            "symfony/flex": true,
            "symfony/runtime": true
        },
        "optimize-autoloader": true,
        "preferred-install": {
            "*": "dist"
        },
        "sort-packages": true
    },
    "autoload": {
        "psr-4": {
            "App\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "App\Tests": "tests/"
        }
    },
    "replace": {
        "symfony/polyfill-ctype": "*",
        "symfony/polyfill-iconv": "*",
        "symfony/polyfill-php72": "*"
    },
    "scripts": {
        "auto-scripts": {
            "cache:clear": "symfony-cmd",
            "assets:install %PUBLIC_DIR%": "symfony-cmd"
        },
        "post-install-cmd": [
            "@auto-scripts"
        ],
        "post-update-cmd": [
            "@auto-scripts"
        ]
    },
    "conflict": {
        "symfony/symfony": "*"
    },
    "extra": {
        "symfony": {
            "allow-contrib": false,
            "require": "5.4.*"
        }
    }
}

解决方案

  • 核心原因:项目依赖的api-platform/core是2.7版本,但当前安装的symfony/maker-bundle^1.43默认生成API Platform 3.x的注解语法,而3.x版本的ApiResource构造函数参数规则和2.x完全不兼容,导致注解解析时参数类型不匹配。

  • 具体修复步骤:

    1. 降级maker-bundle到适配API Platform 2.x的版本,执行命令:
      composer require symfony/maker-bundle:^1.39 --dev
      
    2. 检查自动生成的实体类,确保@ApiResource注解的导入是2.x版本的命名空间:
      use ApiPlatform\Core\Annotation\ApiResource;
      
      而非3.x版本的ApiPlatform\Metadata\ApiResource。
    3. 清理项目缓存:
      php bin/console cache:clear
      
    4. 重新执行make:entity命令,此时生成的注解语法会适配API Platform 2.x版本,不会出现参数类型错误。

内容的提问来源于stack exchange,提问作者sridhar pandurangiah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 01:17:35