Symfony相同的@groups在两个表之间具有manymany关系



我想在两个表之间有相同的@groups与manymany关系:当我得到API平台…/api/tags/1,我只收到没有"tag"

{
"id": 1,
"title": "A ce monde que tu fais"
}

应用实体歌

/**
* @Groups({"song:read", "song:write"})
* @ORMManyToMany(targetEntity=Tag::class, inversedBy="songs", cascade={"persist"})
* @ORMJoinTable(
*  name="song_tag",
*  joinColumns={
*      @ORMJoinColumn(name="song_id", referencedColumnName="id")
*  },
*  inverseJoinColumns={
*      @ORMJoinColumn(name="tag_id", referencedColumnName="id")
*  })
* 
*/
private $tags;

应用 实体标记

/**
* @Groups({"song:read", "song:write"})
* @ORMManyToMany(targetEntity=Song::class, mappedBy="tags")
*/
private $songs;

我认为这是两者之间的连接表,它没有一个定义的组。你能帮我吗?由于

你说:

是什么意思?

当我进入API平台…/api/tags/1,我只收到没有"tag"

据我所知,您的问题可能是缺少规范化上下文配置的结果。Tag端点,如/api/tags/1,默认不配置为应用song:readsong:write序列化组,导致这些Tag字段被排除在响应负载中。

考虑添加song:readsong:write作为Tag端点的默认序列化组。或者,最好指定tag:readtag:write序列化组,并将它们添加到Song序列化组中。一个简单的例子:

<?php declare(strict_types = 1);
namespace AppEntity;
use ApiPlatformCoreAnnotationApiResource;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;
/**
* @ORMEntity
* @ApiResource(
*      itemOperations={
*          "get"={
*              "normalization_context"={
*                  "groups"={
*                      "song:read",
*                  },
*              },
*          },
*      },
* )
*/
class Song
{
/**
* @Groups({
*     "song:read",
*     "tag:read",
* })
*/
private Collection $tags;
}
/**
* @ORMEntity
* @ApiResource(
*      itemOperations={
*          "get"={
*              "normalization_context"={
*                  "groups"={
*                      "tag:read",
*                  },
*              },
*          },
*      },
* )
*/
class Tag
{
/**
* @Groups({
*     "tag:read",
*     "song:read",
* })
*/
private Collection $songs;
}

PS:考虑上面的例子建立了一个循环引用。

by

当我进入API平台…/api/tags/1,我只收到没有"tag"

我的意思是当我发出请求时,我想要得到标签。我已经试过了,它适用于oneToMany和oneToOne,但不适用于manymany。

我想要

{
"id": 1,
"title": "world",
"category": {"id":54, "name":"hello"},
"tags": [{"id":12, "name":"city"}, {...}]
}

,我只有

{
"id": 1,
"title": "world",
"category": {"id":54, "name":"hello"},
}

我认为这是两者之间的连接表,它没有定义组,因为symfony没有创建实体,因此没有组…

最新更新