API 平台 - 没有为资源类型定义标识符

API Platform - No identifiers defined for resource of type

我有一个由 iso_code 属性标识的学说实体 Class :

/**
 * @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;
    }
}

当我尝试通过 GET 请求访问列表时 /api/v2/languages?pages=1,出现 "No identifiers defined for resource of type \"App\Entity\Language\"" 错误。

当我尝试通过 GET 请求访问项目时 /api/v2/languages/en,我得到了 Invalid identifier value or configuration

配置: - api 平台 v2.5.6 - php 7.4 - symfony 4.4

将标识符的名称更改为 $id 就成功了。

但我不明白为什么不能使用 $iso_code

你必须添加

App\Entity\Language
  properties: 
    id: 
      identifier: false
    iso_code:
      identifier: true 

通过在 getter 函数