I have Category
OneToMany
Post
association in doctrine2 setup like this:
Category:
...
/**
* @ORM\OneToMany(targetEntity="Post", mappedBy="category")
* @Type("ArrayCollection<Platform\BlogBundle\Entity\Post>")
*/
protected $posts;
...
Post:
...
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="posts")
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
* @Type("Platform\BlogBundle\Entity\Category")
*/
protected $category;
...
I am trying to deserialize following json object (both entities with id of 1 already exist in database)
{
"id":1,
"title":"Category 1",
"posts":[
{
"id":1
}
]
}
using JMSSerializerBundle serializer's deserialize method configured with doctrine object constructor
jms_serializer.object_constructor:
alias: jms_serializer.doctrine_object_constructor
public: false
with following result:
Platform\BlogBundle\Entity\Category {#2309
#id: 1
#title: "Category 1"
#posts: Doctrine\Common\Collections\ArrayCollection {#2314
-elements: array:1 [
0 => Platform\BlogBundle\Entity\Post {#2524
#id: 1
#title: "Post 1"
#content: "post 1 content"
#category: null
}
]
}
}
Which is fine at first sight. The problem is, that associated Post
has category
field set to null
, resulting in no association on persist()
. If I try to deserialize this:
{
"id":1,
"title":"Category 1",
"posts":[
{
"id":1
"category": {
"id":1
}
}
]
}
it works fine but that is not what I want to do :( I suspect that solution could be to somehow reverse the order in which entities are saved. If the post was saved first and category second, this should work.
How to save this association properly?