symfonyentitybackapi-platform.com

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

  • 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 that works well. Then you can add you serialization groups to the id.

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