Api-platform: No identifiers defined for resource of type

Created on 9 Jun 2020  路  3Comments  路  Source: api-platform/api-platform

API Platform version(s) affected: 2.5.6

  • php 7.4
  • symfony 4.4

Description
I have an Doctrine Entity Class identified by an iso_code attribute :

<?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ApiResource(
 *     graphql={
 *          "item_query",
 *          "collection_query"
 *     }
 * )
 *
 * @ORM\Entity(repositoryClass="App\Repository\LanguageRepository")
 * @ORM\HasLifecycleCallbacks
 */
class Language extends BaseEntity
{
    /**
     * @var string
     * @ApiProperty(identifier=true)
     *
     * @ORM\Id()
     * @ORM\Column(type="string", length=2, unique=true)
     */
    private $iso_code;

    public function getId(): string
    {
        return $this->iso_code;
    }

    public function getIsoCode(): string
    {
        return $this->iso_code;
    }
}

I though the @ApiProperty(identifier=true) annotation would have done the trick but it doesn't.
I also tried to add a getId() method but without success.

When I try to access a list through a GET requests /api/v2/languages?pages=1, I got "No identifiers defined for resource of type \"App\\Entity\\Language\"" error.

When I try to access an item through a GET requests /api/v2/languages/en, I got Invalid identifier value or configuration

I looked for 2 hours now in the documentation and internet but can't find why.

Most helpful comment

Hi I faced similar problem today, I fixed it by making the id key public

here is my working example :

class Book
{
    /**
     * @var int
     *
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ApiProperty(identifier=true)
     * @ORM\Id
     */
    public $book_id;

All 3 comments

did you find out ?

@ADcreatif Nope.

The only way to make this work was to rename $iso_code to $id.

Hi I faced similar problem today, I fixed it by making the id key public

here is my working example :

class Book
{
    /**
     * @var int
     *
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ApiProperty(identifier=true)
     * @ORM\Id
     */
    public $book_id;
Was this page helpful?
0 / 5 - 0 ratings

Related issues

benbender picture benbender  路  4Comments

PierreNAPOLETANO picture PierreNAPOLETANO  路  3Comments

kswzr picture kswzr  路  3Comments

maximilienGilet picture maximilienGilet  路  3Comments

dunglas picture dunglas  路  3Comments