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

Symfony中实体属性重命名后无法正确保存的问题排查

问题排查:API-Platform返回实体新旧属性共存

问题背景

我需要将TestEntity类的属性从驼峰命名的$testMe改为蛇形命名的$test_me,修改前后代码如下:

初始代码

class TestEntity
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 255)]
    private ?string $testMe = null;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getTestMe(): ?string
    {
        return $this->testMe;
    }

    public function setTestMe(string $testMe): self
    {
        $this->testMe = $testMe;

        return $this;
    }
}

修改后的代码

class TestEntity
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 255)]
    private ?string $test_me = null;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getTestMe(): ?string
    {
        return $this->test_me;
    }

    public function setTestMe(string $test_me): self
    {
        $this->test_me = $test_me;

        return $this;
    }
}

异常现象

修改完成并清理Symfony缓存、Doctrine缓存后,API-Platform返回的JSON中同时存在新旧两个属性:

[
  {
      "test_me": "test",
      "testMe": "test"
    }
]

项目中除了TestEntity类的getter/setter外,没有其他testMe的引用,不清楚旧属性为何仍被返回,请求排查原因。

环境配置(composer.json)

{
  "type": "project",
  "license": "proprietary",
  "minimum-stability": "stable",
  "prefer-stable": true,
  "require": {
    "php": ">=8.1",
    "ext-ctype": "*",
    "ext-iconv": "*",
    "api-platform/core": "^3.1",
    "doctrine/annotations": "^2.0",
    "doctrine/doctrine-bundle": "^2.8",
    "doctrine/doctrine-migrations-bundle": "^3.2",
    "doctrine/orm": "^2.14",
    "easycorp/easyadmin-bundle": "*",
    "lexik/jwt-authentication-bundle": "^2.18",
    "nelmio/cors-bundle": "^2.3",
    "phpdocumentor/reflection-docblock": "^5.3",
    "phpstan/phpdoc-parser": "^1.16",
    "sensio/framework-extra-bundle": "*",
    "symfony/apache-pack": "^1.0",
    "symfony/asset": "6.2.*",
    "symfony/console": "6.2.*",
    "symfony/debug-bundle": "6.2.*",
    "symfony/doctrine-messenger": "6.2.*",
    "symfony/dotenv": "6.2.*",
    "symfony/expression-language": "6.2.*",
    "symfony/flex": "^2",
    "symfony/framework-bundle": "6.2.*",
    "symfony/http-client": "6.2.*",
    "symfony/mailer": "6.2.*",
    "symfony/messenger": "6.2.*",
    "symfony/monolog-bundle": "*",
    "symfony/notifier": "6.2.*",
    "symfony/property-access": "6.2.*",
    "symfony/property-info": "6.2.*",
    "symfony/runtime": "6.2.*",
    "symfony/security-bundle": "6.2.*",
    "symfony/sendgrid-mailer": "6.2.*",
    "symfony/serializer": "6.2.*",
    "symfony/twig-bundle": "6.2.*",
    "symfony/validator": "6.2.*",
    "symfony/web-profiler-bundle": "6.2.*",
    "symfony/yaml": "6.2.*",
    "symfonycasts/verify-email-bundle": "*",
    "twig/extra-bundle": "*",
    "twig/twig": "^2.12|^3.0"
  },
  "config": {
    "allow-plugins": {
      "php-http/discovery": true,
      "symfony/flex": true,
      "symfony/runtime": true
    },
    "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": "*",
    "symfony/polyfill-php73": "*",
    "symfony/polyfill-php74": "*",
    "symfony/polyfill-php80": "*",
    "symfony/polyfill-php81": "*"
  },
  "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": "6.2.*"
    }
  },
  "require-dev": {
    "symfony/maker-bundle": "^1.48"
  }
}

排查方向

  • 序列化器自动识别:Symfony Serializer会自动识别getter方法,getTestMe()会被解析为testMe属性,可尝试修改getter名称为getTestMe()对应蛇形输出,或者在实体上显式配置序列化属性。
  • API资源元数据缓存:运行php bin/console api:cache:clear清理API-Platform独立的资源元数据缓存。
  • 数据库字段同步:执行php bin/console doctrine:schema:validate验证Doctrine映射与数据库字段是否完全同步。
  • 命名策略配置:检查config/packages/api_platform.yaml中的属性命名策略,是否同时启用了多种转换规则导致重复序列化。
  • Profiler调试:用Symfony Web Profiler查看序列化流程,确认属性来源是实体字段还是getter方法。

内容的提问来源于stack exchange,提问作者Ed Ke

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 10:35:16