错误:无法为 XXX 实体生成 IRI

问题描述 投票:0回答:1

首先对不起我的英语。

我正在尝试使用 API 平台和 Symfony 对我的客户实体进行 POST。 (获取工作正常)

在进行 POST 时,我发现自己遇到了这个错误:

“无法为“App\Entity\Customer”生成 IRI。”

这是我的实体,我认为问题来自这里,但我找不到原因。

<?php

namespace App\Entity;

use ....


/**
 * @ORM\Entity(repositoryClass=CustomerRepository::class)
 */
#[ApiResource(
    collectionOperations: [
        'get' => ['method' => 'get'],
        'post' => ['method' => 'post'],
    ],
    itemOperations: [
        'get' => ['method' => 'get'],
        'put' => ['method' => 'put'],
        'delete' => ['method' => 'delete'],
    ],
    denormalizationContext: ['groups' => ['write_customer']],
    normalizationContext: ['groups' => ['read_customer']],
)]

class Customer
{
......
symfony entity back api-platform.com
1个回答
0
投票

生成 IRI 需要 ID 的可见性,如果您有序列化组,则必须将此组添加到 id (primKey)。

首先确保您有一个运行良好的

getId
功能。然后您可以将序列化组添加到 id 中。

您可以在文档中找到示例:https://api-platform.com/docs/core/serialization/#calculated-field

....
    denormalizationContext: ['groups' => ['write_customer']],
    normalizationContext: ['groups' => ['read_customer']],
....




/**
 * @var int The entity Id
 *
 * @ORM\Id
 * @ORM\GeneratedValue
 * @ORM\Column(type="integer")
 */
#[Groups("write_customer", read_customer)]
private $id;
© www.soinside.com 2019 - 2024. All rights reserved.