API 不返回自动生成的 ID

API not returning auto generated ID

我在 Symfony API 平台上工作,添加和检索一些东西。 Table 有两个字段 id 和 title。 但是当我 运行 GET 查询 API 是 returning title only not id.

如何return ID也一样?

我的注释:-

 * @ORM\Table(
*      name="school",
* @ApiResource(
*     attributes={
*         "order"={"title": "ASC"},
*         "normalization_context"={"groups"={"school.read"}, 
"enable_max_depth"=true},
*     },
*     itemOperations={
*         "get",
*         "put"
*     },
*     collectionOperations={
*          "get"={
*              "normalization_context"={
*                  "groups"={"school.read"}
*              }
*          }
*     },
*     normalizationContext={
*          "groups"={"school.read"}
*     },
*     denormalizationContext={
*          "groups"={"school.write"}
*     }
* )
* @ORM\Entity(repositoryClass="Eqsgroup\Repository\SchoolRepository")
* @UniqueEntity(
*      "title",
*      repositoryMethod="findByUniqueCriteria",
*      message="School already exists."
* )
*/

这是实体class

class School 
 {

/**
 * @var string the id of this School
 *
 * @ORM\Id
 * @ORM\Column(type="guid", unique=true)
 * @ORM\GeneratedValue(strategy="CUSTOM")
 * @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator")
 * @Groups({"school.read, school.write"})
 */
private $id;

/**
 * @var string The title of the school
 *
 * @ORM\Column(type="string", length=255)
 * @Assert\NotNull(message="school should not be empty")
 * @Assert\NotBlank(message="school should not be empty")
 * @Assert\Length(
 *      min = 1,
 *      max = 250,
 *      minMessage = "length.min,{{ limit }}",
 *      maxMessage = "length.max,{{ limit }}"
 * )
 * @Groups({"school.read", "school.write"})
 */
private $title;

public function __construct(){ }

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

public function getTitle(): ?string
{
    return $this->title;
}

public function setTitle(string $title): self
{
    $this->title = $title;

    return $this;
}

这是当前得到的输出:

[
{
 "title": "Test"
},
{
 "title": "Test2"
},

]

预期输出将包括自动生成的标题。

添加@Groups 以允许Apli-Platform 读取您想要的每个字段,如下所示:

/**
 * @ORM\Id()
 * @ORM\GeneratedValue()
 * @ORM\Column(type="integer")
 * @Groups({"school.read", "school.write"})
 */
private $id;

查看此处的文档: https://api-platform.com/docs/core/serialization/#the-serialization-context-groups-and-relations

您是否配置了要公开的属性?

如果没有,请在实体的 yaml 中配置它:

# I don't know the path to your entity, just modify what you need to
XXXX\XXXX\XXXX\XXXX\School:
    exclusion_policy: ALL
    properties:
        id:
            expose: true
        title:
            expose: true