我Category OneToMany Post在doctrine2设置中有这样的关联:
类别:
...
/**
* @ORM\OneToMany(targetEntity="Post", mappedBy="category")
* @Type("ArrayCollection<Platform\BlogBundle\Entity\Post>")
*/
protected $posts;
...
Run Code Online (Sandbox Code Playgroud)
帖子:
...
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="posts")
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
* @Type("Platform\BlogBundle\Entity\Category")
*/
protected $category;
...
Run Code Online (Sandbox Code Playgroud)
我试图反序列化后面的json对象(数据库中已存在id为1的两个实体)
{
"id":1,
"title":"Category 1",
"posts":[
{
"id":1
}
]
}
Run Code Online (Sandbox Code Playgroud)
使用JMSSerializerBundle序列化程序的deserialize方法配置了doctrine对象构造函数
jms_serializer.object_constructor:
alias: jms_serializer.doctrine_object_constructor
public: false
Run Code Online (Sandbox Code Playgroud)
结果如下:
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
}
] …Run Code Online (Sandbox Code Playgroud)