API 不返回自动生成的 ID



我在Symfony API平台上工作,添加和检索一些东西。表有两个字段 id 和标题。但是当我运行 GET 查询时,API 只返回标题而不是 id。

如何也返回 ID?

我的注释:-

 * @ORMTable(
*      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"}
*     }
* )
* @ORMEntity(repositoryClass="EqsgroupRepositorySchoolRepository")
* @UniqueEntity(
*      "title",
*      repositoryMethod="findByUniqueCriteria",
*      message="School already exists."
* )
*/

这是实体类

class School 
 {
/**
 * @var string the id of this School
 *
 * @ORMId
 * @ORMColumn(type="guid", unique=true)
 * @ORMGeneratedValue(strategy="CUSTOM")
 * @ORMCustomIdGenerator(class="RamseyUuidDoctrineUuidGenerator")
 * @Groups({"school.read, school.write"})
 */
private $id;
/**
 * @var string The title of the school
 *
 * @ORMColumn(type="string", length=255)
 * @AssertNotNull(message="school should not be empty")
 * @AssertNotBlank(message="school should not be empty")
 * @AssertLength(
 *      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"
},

]

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

添加@Groups以允许Apli-Platform像这样读取您想要的每个字段:

/**
 * @ORMId()
 * @ORMGeneratedValue()
 * @ORMColumn(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
XXXXXXXXXXXXXXXXSchool:
    exclusion_policy: ALL
    properties:
        id:
            expose: true
        title:
            expose: true

相关内容

  • 没有找到相关文章