首先对不起我的英语。
我正在尝试使用 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
{
......
生成 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;