[英]Create entity with relation on a POST request with Api Platform
I want to send a post request that creates an entity and also other entities from one-to-many relations.我想发送一个帖子请求,该请求创建一个实体以及一对多关系中的其他实体。 I have a bet_question, that can be related to many bet_choices and many bet_points, and I want to create a bet_question with some bet_choices and bet_points in the same request.
我有一个 bet_question,它可以与许多 bet_choices 和许多 bet_points 相关,我想在同一个请求中创建一个包含一些 bet_choices 和 bet_points 的 bet_question。 I used serialization groups like written in the doc, but I still get this error: Nested documents for attribute "betPoints" are not allowed.
我使用了文档中所写的序列化组,但我仍然收到此错误:不允许属性“betPoints”的嵌套文档。 Use IRIs instead.
请改用 IRI。
Here is what I send (with accept: application/json
in 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"
}
And here is my entities:这是我的实体:
Truncated Entity BetQuestion截断的实体 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;
Truncated Entity BetPoint截断实体 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;
Truncated Entity BetChoice截断实体 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;
Can you help me find what I am doing wrong please?你能帮我找到我做错了什么吗?
You have a syntax error in the annotation:注释中存在语法错误:
/**
* @ApiResource(attributes={
* "normalizationContext"={"groups"={"bet:read"}},
* "denormalizationContext"={"groups"={"bet:write"}}
* })
*/
It should be:它应该是:
/**
* @ApiResource(
* normalizationContext={"groups"={"bet:read"}},
* denormalizationContext={"groups"={"bet:write"}},
* )
*/
Which is also equivalent to:这也相当于:
/**
* @ApiResource(attributes={
* "normalization_context"={"groups"={"bet:read"}},
* "denormalization_context"={"groups"={"bet:write"}},
* })
*/
Reference: https://api-platform.com/docs/core/serialization/#using-serialization-groups参考: https://api-platform.com/docs/core/serialization/#using-serialization-groups
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.