【问题标题】:API Platform - No identifiers defined for resource of typeAPI 平台 - 没有为资源类型定义标识符
【发布时间】:2020-12-22 15:46:30
【问题描述】:

我有一个由 iso_code 属性标识的 Doctrine 实体类:

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

【问题讨论】:

  • 抱歉,它是 api-pack 的 1.2.2
  • 在 Symfony 中,每个实体都必须有一个标识符/主键,在您的情况下,您必须有两个变量,一个用于 ID,必须有一个 GeneratedValue 作为注释,另一个是 iso 代码
  • API 平台之外的每一项工作,ISO_CODE 都是唯一的primary_key。我在文档中没有看到我必须使用 GeneratedValue 的任何地方?

标签: symfony api-platform.com


【解决方案1】:

你必须添加

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

【讨论】:

    【解决方案2】:

    通过在顶部设置标识符 @ApiProperty(identifier=true) 解决了类似问题 获取函数

    【讨论】:

      【解决方案3】:

      将标识符的名称更改为 $id 就可以了。

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

      【讨论】:

        猜你喜欢
        • 2012-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-01
        • 1970-01-01
        • 2011-11-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多