某些OneToMany关系作为对象返回,而某些则作为IRI-API平台返回

问题描述 投票:0回答:1

当我向Form提出请求时,我希望将相关的问题和问题组作为IRI的列表。

我的实体很少:

  • 表格(字段:question_groups,问题)
  • QuestionGroup(字段:表单,问题)
  • 问题(字段:表单,问题组)

当我获取表单请求时,它返回:

  • 表格
    • 问题['/ api / questions / 1',...]
    • questionGroups [{id:1,name:'foo',questions:['/ api / questions / 1',...] ...},...]

您可以看到,Form.questions是IRI的列表,而Form.questionGroups是对象的列表。我想让他们两个都作为IRI。

在下面的问题组字段下的图像上有一个问题组字段,但是在问题或答案下没有问题字段,...

enter image description here

整个东西对我来说毫无意义,我尝试设置@MaxDepth,它什么都没有改变(除非当我使用@MaxDepth(0)时,它在响应中或在php日志中抛出500错误而没有任何错误消息)

任何人都可以解释原因,我应该怎么做才能将问题和问题组作为IRI列表加载?

谢谢

这里是上述实体的相关部分。

/**
 * @ORM\Entity
 * @ApiResource
 * @ORM\HasLifecycleCallbacks
 * @Gedmo\SoftDeleteable(fieldName="deleted_at", timeAware=false, hardDelete=true)
 */
class Form
{
    /**
     * @ORM\Column(type="text", length=200, nullable=false)
     */
    private $name;
    /**
     * @ORM\OneToMany(targetEntity="QuestionGroup", mappedBy="form", cascade={"REMOVE"})
     */
    private $question_groups;
    /**
     * @ORM\OneToMany(targetEntity="Question", mappedBy="form", cascade={"REMOVE"})
     */
    private $questions;


}

/**
 * @ORM\Entity
 * @ApiResource
 * @Gedmo\SoftDeleteable(fieldName="deleted_at", timeAware=false, hardDelete=true)
 */
class QuestionGroup
{
    /**
     * @ORM\Column(type="string", length=200, nullable=false)
     */
    private $name;
    /**
     * @ORM\ManyToOne(targetEntity="Form",inversedBy="question_groups")
     */
    private $form;
    /**
     * @ORM\OneToMany(targetEntity="Question", mappedBy="question_group", cascade={"REMOVE"})
     */
    private $questions;
}

/**
 * @ORM\Entity
 * @ApiResource
 * @Gedmo\SoftDeleteable(fieldName="deleted_at", timeAware=false, hardDelete=true)
 */
class Question
{
    /**
     * @ORM\Column(type="string", length=200, nullable=false)
     */
    private $name;
    /**
     * @ORM\ManyToOne(targetEntity="Form",inversedBy="questions")
     */
    private $form;
    /**
     * @ORM\ManyToOne(targetEntity="QuestionGroup", inversedBy="questions")
     */
    private $question_group;
}
symfony api-platform.com
1个回答
0
投票

我已经知道,如果Entity字段/属性是像“ $ question_groups”这样的snake_case,那么API将把它作为对象列表返回,如果它的camelCase像“ $ questionGroups”那样,它将作为IRI列表返回。旁注:normalisationContext不喜欢snake_case。如果您使用normalizationContext并且该属性为snake_cased,则它将不包含在响应数据中。

© www.soinside.com 2019 - 2024. All rights reserved.