我有两个实体: Tag,Post。在多对多关系中
我在GET: / API /tags/{id}中有一个api路由
我想通过标签返回与标签关联的帖子。但我也想返回与由该标记返回的posts对象相关联的标记。
这会产生一个错误:
“联接关系的总数已超过指定的最大值。如有必要,请使用"api_platform.eager_loading.max_joins”配置键(https://api-platform.com/docs/core/performance/#eager-loading)提高限制,或使用Symfony序列化程序(https://symfony.com/doc/current/components/serializer.html#handling-serialization-depth)的"enable_max_depth“选项限制最大序列化深度。”
标签实体:
#[ApiResource(
normalizationContext: ['groups' => ['read:collection:Tag']],
collectionOperations:['get'],
paginationEnabled: false,
itemOperations: [
'get' => [
'normalization_context' => ['groups' => [
'read:item:Tag',
'read:collection:Tag'
]]
],
]
)]
class Tag
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
#[Groups(['read:collection:Post', 'read:item:Category', 'read:collection:Tag'])]
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
#[Groups(['read:collection:Post', 'read:item:Category', 'read:collection:Tag'])]
private $name;
/**
* @ORM\ManyToMany(targetEntity=Post::class, mappedBy="tags")
*/
#[Groups(['read:item:Tag'])]
private $posts;Post实体中的tag字段:
/**
* @ORM\ManyToMany(targetEntity=Tag::class, inversedBy="posts")
*/
#[Groups(['read:collection:Post', 'read:item:Category','read:item:Tag'])]
private $tags;我尝试使用错误消息中指示的"enable_max_depth“选项限制最大序列化深度,但不起作用。
发布于 2021-07-07 23:13:02
我在多对多关系中遇到过这个问题,当同一个序列化组既用于实体的relationship属性,也用于关联实体的相关属性。
在您的示例中,我认为您需要从Post实体的$tags属性中删除read:item:Tag组。
/**
* @ORM\ManyToMany(targetEntity=Tag::class, inversedBy="posts")
*/
#[Groups(['read:collection:Post', 'read:item:Category'])]
private $tags;我意识到这可能会影响从posts端点检索数据的方式,但为了避免递归,这似乎对于关系是必要的。组基础设施可能会变得令人困惑。
https://stackoverflow.com/questions/68286180
复制相似问题