如何使用Api平台对自定义实体使用自定义id列?

问题描述 投票:1回答:1

我正在使用Symfony 4和API平台,我想为Doctrine实体使用自定义标识符。读the documentation,看起来非常简单:

/**
 * @ApiResource
 */
final class Person
{
    /**
     * @var Uuid
     * @ApiProperty(identifier=true)
     */
    public $code;

    // ...
}

这就是我想使用的方式:

/**
 * @ApiResource
 */
final class User
{
    /**
     * @var string
     * @ApiProperty(identifier=true)
     */
    public $username;

    // ...
}

但是它不起作用:它找不到项目(我收到404响应)。我也尝试创建一个custom operation,但是我也无法使其正常工作。

我在GitHub(123)上发现了一些有关此问题,但我不明白为什么它不起作用。对我来说,这看起来像是一个非常基本的功能,我认为这种情况(希望使用/api/users/{username}而不是/api/users/{id})不是边缘情况。

[id字段以外的配置标识符的最简单方法是什么?

php symfony doctrine-orm doctrine api-platform.com
1个回答
0
投票

该实体中没有ORM映射,因此,Doctrine无法知道哪个字段是该实体的唯一标识符。

基本示例:

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ApiResource
 */
final class User
{
    /**
     * @ORM\Id
     * @ORM\Column(type="string", nullable=false)
     * @var string
     * @ApiProperty(identifier=true)
     */
    public $username;

    // ...
}
© www.soinside.com 2019 - 2024. All rights reserved.