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

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

我想发送一个发帖请求,该请求根据一对多关系创建一个实体以及其他实体。我有一个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"
}

这是我的实体:

截短的实体投注问题

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

截短的实体选择

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

您能帮我发现我在做什么错吗?

php symfony api-platform.com
1个回答
0
投票

您在注释中存在语法错误:

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

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