Reputation: 99
I have set up a server based on API-Platform.
I autogenerated some Entities wit the symfony maker-bundle and modified them to fit my needs.
If I call the API with a POST-Request I get an Error Uncaught PHP Exception Symfony\Component\Serializer\Exception\NotNormalizableValueException: "Update is not allowed for this operation."
.
As I understand, it is not allowed to update related entities this way. But that's not what I intend. I just want to establish the relationship
What am I doing wrong?
The following is a simplified version of my problem. I left out the autogenerated getters/setters.
The entities:
/**
* @ApiResource(
* normalizationContext={"groups"={"book"}},
* denormalizationContext={"groups"={"book"}}
* )
*/
class Book
{
/**
* @var integer
*
* @Groups({"book"})
*
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer", options={"unsigned"=true})
*/
private $id;
/**
* @var string
*
* @Groups({"book"})
*
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $name;
/**
* @var Language
*
* @Groups({"book"})
*
* @ORM\ManyToOne(targetEntity="App\Entity\Language", inversedBy="books")
* @ORM\JoinColumn(nullable=false)
*/
private $language;
}
/**
* @ApiResource(normalizationContext={"groups"={"language", "book"}})
*/
class Language
{
/**
* @var integer
*
* @Groups({"language", "book"})
*
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer", options={"unsigned"=true})
*/
private $id;
/**
* @var string
*
* @Groups({"language", "book"})
*
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @var Book
*
* @ORM\OneToMany(targetEntity="App\Entity\Book", mappedBy="language")
*/
private $books;
}
If I send the following POST-request to the API, to create a Book that has a already existing Language
{
'name': 'some Name',
'language': {
'id': 234,
'name': "some languages name"
}
}
the API responds with this error
Uncaught PHP Exception Symfony\Component\Serializer\Exception\NotNormalizableValueException: "Update is not allowed for this operation."
Upvotes: 1
Views: 3714
Reputation: 11
I dunno if you still interessed on this, but, unless you do not mind to build a normalizer for the Book
, create another name for the id
. Seems that is "reserved" for IRI use (the response comes with a @id
attribute).
See the discution and some solutions on the github issue.
Upvotes: 0
Reputation: 91
If I understand it correctly, you want to make - update of the existing Language - and assing it to a newly created Book in the same api operation?
This probably will really not work. You can use two operations (update and assign via IRI like "/api/language/234"
). Or you can create a new Book and Language at once.
Anyway all languages should be pre-created and then only selected ones to be assigned to Books - then no name change should be required during a new Book create action? :-)
Upvotes: 1
Reputation: 1283
I guess there is no need for
/**
* @var Book
*
* @ORM\OneToMany(targetEntity="App\Entity\Book", mappedBy="language")
*/
private $books;
in your Language
entity because it's ManyToOne relation so if Language entity doesn't have that won't matter the working.
Also add denormalization context to your language entity or just remove it from both entities.
Upvotes: 1