“Renamed property not being saved correctly in Symfony”不会保存正确。

huangapple go评论52阅读模式
英文:

Renamed property not being saved correctly in symfony

问题

我已经翻译了你提供的内容,以下是翻译好的部分:

"我试图更新我的实体的属性。例如,在TestEntity类中,我犯了一个错误,使用了snake_case命名约定,我想将$testMe属性重命名为$test_me。"

"在更新后的文本中,我更正了一些大写和标点符号,并为了清晰度重新表述了一些部分。"

"但是,在更新代码后,我注意到旧属性和新属性都会以数组的形式返回(使用API-Platform):"

"我已经清除了缓存和Doctrine缓存,项目中除了TestEntity类中的getter和setter方法之外,没有其他使用testMe的地方。我不确定旧属性被保存在哪里以及原因。你能帮助我理解这个问题吗?"

"这是我的composer.json文件,用于查看已安装的Bundle:"(composer.json未提供具体内容)

英文:

I am trying to update the properties of my entity. For example, in the TestEntity class:

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;
    }
}

I made a mistake with the snake_case naming convention, and I want to rename the $testMe property to $test_me."

In the updated text, I made some corrections to the capitalization and punctuation, and rephrased some parts of the text for clarity.

"I renamed the $testMe property to $test_me everywhere in the TestEntity class:

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;
    }
}

However, after updating the code, i noticed that both the old and the new properties are being returned in an array (using API-Platform):

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

I have cleared the cache and Doctrine cache, and there is no other use of testMe in the project except for the getter and setter methods in the TestEntity class. I am not sure where the old property is being saved and why. Can you help me understand this issue?"

My composer.json to see my installed Bundles

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

答案1

得分: 0

可能存在着关于实体的教义缓存包含过时信息的情况。在这种情况下,您可以尝试通过在控制台中运行以下命令来清除教义缓存:

php bin/console doctrine:cache:clear-metadata

这个命令会删除所有教义元数据缓存文件,使教义能够使用最新信息重新构建缓存。

英文:

it is possible that the doctrine cache contains outdated information about the entity. In this case, you can try clearing the doctrine cache by running the following command in the console

php bin/console doctrine:cache:clear-metadata

This command removes all doctrine metadata cache files, allowing doctrine to rebuild the cache with the most recent information.

答案2

得分: 0

Symfony Serializer 也会查找公共 getter 方法。

链接:
https://symfony.com/doc/current/components/serializer.html#converting-property-names-when-serializing-and-deserializing

https://symfonycasts.com/screencast/api-platform2/serialization-tricks#controlling-field-names-serializedname

尝试 #[SerializedName('test_me')]

英文:

Symfony Serializer also looks to the public-getter method.

https://symfony.com/doc/current/components/serializer.html#converting-property-names-when-serializing-and-deserializing

https://symfonycasts.com/screencast/api-platform2/serialization-tricks#controlling-field-names-serializedname

Try #[SerializedName('test_me')]

huangapple
  • 本文由 发表于 2023年4月13日 23:15:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76007124.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定