一些 OneToMany 关系作为对象返回,一些作为 IRIs - API-Platform

Some OneToMany relationships are returned as objects and some as IRIs - API-Platform

我想在向表单发出获取请求时将相关问题和问题组作为 IRI 列表包括在内。

我的实体很少:

当我对表单进行获取请求时,它 returns:

如您所见,Form.questions 是 IRI 列表,而 Form.questionGroups 是对象列表。我想将它们都作为 IRI。

在下图中,questionGroups 字段下有一个 questionGroup 字段,但问题下没有问题字段,答案下没有答案,...

整个事情对我来说毫无意义,我尝试设置@MaxDepth,它没有任何改变(除了当我使用@MaxDepth(0)时抛出500错误而没有任何错误消息作为响应或在php日志)

谁能解释为什么,我应该怎么做才能将问题和问题组加载为 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;
}

我发现如果实体 field/property 是 snake_case 就像 "$question_groups" 那么 API 将 return 它作为对象列表并且如果它的驼峰命名法类似于“$questionGroups”,它将被 return 编辑为 IRI 列表。旁注:normalisationContext 不喜欢 snake_case。如果您使用 normalisationContext 并且 属性 是 snake_cased,则它不会包含在响应数据中。