首页 > 解决方案 > 使用 Api 平台在 POST 请求上创建具有关系的实体

问题描述

我想发送一个发布请求,该请求创建一个实体以及一对多关系中的其他实体。我有一个 bet_question,它可能与许多 bet_choices 和许多 bet_points 相关,我想在同一个请求中创建一个带有一些 bet_choices 和 bet_points 的 bet_question。我使用了文档中所写的序列化组,但仍然出现此错误:不允许属性“betPoints”的嵌套文档。请改用 IRI。

这是我发送的内容(带有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;

截断实体 BetPoint

/**
 * @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;

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

标签: phpsymfonyapi-platform.com

解决方案


注释中有语法错误:

/**
 * @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


推荐阅读