使用 Api 平台在 POST 请求上创建具有关系的实体

Create entity with relation on a POST request with Api Platform

我想发送一个 post 请求来创建一个实体以及来自 one-to-many 关系的其他实体。我有一个 bet_question,它可以与许多 bet_choices 和许多 bet_points 相关,我想用一些 bet_choices 和 [=43 创建一个 bet_question =] 在同一个请求中。我使用了文档中所写的序列化组,但我仍然收到此错误:不允许属性 "betPoints" 的嵌套文档。请改用 IRI。

这是我发送的内容(header 中有 accept: application/json):

{
   "question":"Will team A win ?",
   "betPoints":[
      {
         "value":"100"
      },
      {
         "value":"200"
      }
   ],
   "betChoices":[
      {
         "name":"Yes",
         "odds":2
      },
      {
         "name":"No",
         "odds":2
      }
   ],
   "fixture":"/api/fixtures/23"
}

这是我的实体:

截断实体 BetQuestion

/**
 * @ApiResource(attributes={
 *         "normalizationContext"={"groups"={"bet:read"}},
 *         "denormalizationContext"={"groups"={"bet:write"}}
 *     })
 * @ORM\Entity(repositoryClass="App\Repository\BetQuestionRepository")
 */
class BetQuestion
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups({"bet:read"})
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"bet:read", "bet:write"})
     */
    private $question;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\BetPoint", mappedBy="betQuestion", cascade={"persist"})
     * @Groups({"bet:read", "bet:write"})
     */
    private $betPoints;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\BetChoice", mappedBy="betQuestion", cascade={"persist"})
     * @Groups({"bet:read", "bet:write"})
     */
    private $betChoices;

    /**
     * @ORM\OneToOne(targetEntity="App\Entity\Fixture", inversedBy="betQuestion", cascade={"persist", "remove"})
     * @Groups({"bet:read", "bet:write"})
     */
    private $fixture;

截断实体投注点

/**
 * @ApiResource()
 * @ORM\Entity(repositoryClass="App\Repository\BetPointRepository")
 */
class BetPoint
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups({"bet:read"})
     */
    private $id;

    /**
     * @ORM\Column(type="integer")
     * @Groups({"bet:read", "bet:write"})
     */
    private $value;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\BetQuestion", inversedBy="betPoints")
     * @ApiSubresource(maxDepth=1)
     */
    private $betQuestion;

截断实体 BetChoice

/**
 * @ApiResource()
 * @ORM\Entity(repositoryClass="App\Repository\BetChoiceRepository")
 */
class BetChoice
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups({"bet:read"})
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"bet:read", "bet:write"})
     */
    private $name;

    /**
     * @ORM\Column(type="boolean")
     * @Groups({"bet:read"})
     */
    private $value = false;

    /**
     * @ORM\Column(type="float")
     * @Groups({"bet:read", "bet:write"})
     */
    private $odds;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\BetQuestion", inversedBy="betChoices")
     * @Groups({"bet:write"})
     */
    private $betQuestion;

你能帮我找出我做错了什么吗?

您的注释存在语法错误:

/**
 * @ApiResource(attributes={
 *     "normalizationContext"={"groups"={"bet:read"}},
 *     "denormalizationContext"={"groups"={"bet:write"}}
 * })
 */

应该是:

/**
 * @ApiResource(
 *     normalizationContext={"groups"={"bet:read"}},
 *     denormalizationContext={"groups"={"bet:write"}},
 * )
 */

也等同于:

/**
 * @ApiResource(attributes={
 *     "normalization_context"={"groups"={"bet:read"}},
 *     "denormalization_context"={"groups"={"bet:write"}},
 * })
 */

参考:https://api-platform.com/docs/core/serialization/#using-serialization-groups