我正在Symfony API平台上进行添加和检索操作。表具有两个字段id和title。 但是,当我运行GET查询时,API仅返回标题,而不返回ID。
如何也返回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 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"
},
]
预期的输出将包括自动生成的标题以及标题。
答案 0 :(得分:1)
添加@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
答案 1 :(得分:1)
您是否配置了要公开的属性?
如果没有,请在您实体的 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