'Error : Unable to generate an IRI for XXX entity

First of all sorry for my english.

I am trying with API Platform and Symfony to do a POST on my customer entity. (the get works fine)

At the time of doing the POST I find myself with this error :

"Unable to generate an IRI for "App\Entity\Customer"."

Here is my entity I think the problem comes from here but I can't find the reason.

<?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
{
......


Solution 1:[1]

Generating the IRI needs visibility on the ID if you have serialization Groups you have to add this groups to id (primKey).

First make sure you have a getId function thats works well. Then you can add you searialisation groups to the id.

Here you can find an example in the dokumetation: 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;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 episch