php - 使用 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;
你能帮我找出我做错了什么吗?
解决方案
注释中有语法错误:
/**
* @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
推荐阅读
- vb.net - VB.NET - 在流关闭之前不发送数据
- r - mutate_all 在 r 中的列表上使用 map 和 case_when / ifelse
- android - 无法进行其他活动
- python - 欧几里得 GCD 函数返回类型 None 而不是 int
- c++ - Steam 和 Blizzard Launcher 之类的东西如何在不使用默认操作系统窗口的情况下创建 GUI?
- python - 'list' 对象不可调用,试图访问列表中的项目并将其分配给对象
- kotlin - Spring DataJpaTest 中的 Kotlin 协程
- excel - 等到在 Outlook 中发送电子邮件,然后再移动到下一个单元格行
- firebase - 如何调试 Google Cloud Functions 中的 Axios 错误?
- javascript - jQuery ajax 创建无限循环