首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用symfony在apiplatform上二次调用关系问题

用symfony在apiplatform上二次调用关系问题
EN

Stack Overflow用户
提问于 2021-07-07 20:35:18
回答 1查看 247关注 0票数 0

我有两个实体: 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“选项限制最大序列化深度。”

标签实体:

代码语言:javascript
复制
#[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字段:

代码语言:javascript
复制
/**
 * @ORM\ManyToMany(targetEntity=Tag::class, inversedBy="posts")
 */
#[Groups(['read:collection:Post', 'read:item:Category','read:item:Tag'])]
private $tags;

我尝试使用错误消息中指示的"enable_max_depth“选项限制最大序列化深度,但不起作用。

EN

回答 1

Stack Overflow用户

发布于 2021-07-07 23:13:02

我在多对多关系中遇到过这个问题,当同一个序列化组既用于实体的relationship属性,也用于关联实体的相关属性。

在您的示例中,我认为您需要从Post实体的$tags属性中删除read:item:Tag组。

代码语言:javascript
复制
/**
 * @ORM\ManyToMany(targetEntity=Tag::class, inversedBy="posts")
 */
#[Groups(['read:collection:Post', 'read:item:Category'])]
private $tags;

我意识到这可能会影响从posts端点检索数据的方式,但为了避免递归,这似乎对于关系是必要的。组基础设施可能会变得令人困惑。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68286180

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档