反序列化过程OneToMany-ManyToOne进程始终将关系数据集设为NULL。
结果实体:
@ORM\OneToMany(targetEntity="\Acme\DemoBundle\Entity\AnswerContent", mappedBy="answerResult", cascade={"persist", "remove"}, orphanRemoval=true)
@JMS\Type("ArrayCollection<Acme\DemoBundle\Entity\AnswerContent>")
@JMS\Groups({"survey_answer_fetching"})
答案实体:
@ORM\ManyToOne(targetEntity="Acme\DemoBundle\Entity\AnswerResult", inversedBy="answers")
@ORM\JoinColumn(name="answer_result_id", referencedColumnName="id")
@JMS\Type("Acme\DemoBundle\Entity\AnswerResult")
这是json:
{"results":
[{"answers":[
{"choices":[{"fieldId":1}],"value":"","questionId":45},
{"choices":[{"fieldId":1}],"value":"","questionId":67}],
"someId":9
}],
"personId":18}
$serializer = $this->get('jms_serializer');
$data = $serializer->deserialize($json['json'], 'Acme\\DemoBundle\\Entity\\AnswerResult', 'json');
反序列化过程就是决定这个价值......
一切都很好,所有json变量都设置了正确的实体属性。
$em->persist($data);
$em->flush();
值传递给DB但关系不是... JMSSerializationBundle的所有文档。所有JMS类型都已定义......
但DB / Doctrine没有映射关系...
答案 0 :(得分:3)
我用merge()函数解决了同样的问题:
$entity = $em->merge($data);
$em->persist($entity);
$em->flush();