这个问题通常发生在你的实体类缺少 @ApiResource
注释或选择了错误的属性来标识实体。
要解决这个问题,请确保你的实体类至少有以下内容:
use ApiPlatform\Core\Annotation\ApiResource;
/**
* @ApiResource
*/
class YourEntityClass
{
// ...
}
同时,你需要在实体类中指定用于标识此实体的属性。例如,如果你的实体类有一个名为 id
的主键属性,则可以在 @ApiResource
注释中将其指定为:
use ApiPlatform\Core\Annotation\ApiResource;
/**
* @ApiResource(
* iri="your/iri/here",
* attributes={
* "id"={"identifier"=true}
* }
* )
*/
class YourEntityClass
{
/**
* @var int
*
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
// ...
}
这样就可以解决 “Api-platform 2.7 - Unable to generate an IRI for the item of type” 的问题了。