Reputation: 100
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. I used serialization groups like written in the doc, but I still get this error : Nested documents for attribute "betPoints" are not allowed. Use IRIs instead.
Here is what I send (with accept: application/json
in header) :
{
"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
/**
* @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
/**
* @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
/**
* @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 ?
Upvotes: 1
Views: 6062
Reputation: 2338
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
Upvotes: 3