我正在创建一个测验应用程序,我有这些实体:
与关系
测验ManyToMany
问题
问题OneToMany
答案
我正在使用序列化实体在客户端上编辑问题 - 答案然后当我尝试保存时,我将JSON反序列化回实体。
实体:
测验:
/**
* @ORM\ManyToMany(targetEntity="Quiz\CoreBundle\Entity\Question", inversedBy="quizes", cascade={"persist", "remove", "merge"})
* @Type("ArrayCollection<Quiz\CoreBundle\Entity\Question>")
* @Groups({"public", "admin"})
*/
protected $questions;
问题:
/**
* @ORM\ManyToMany(targetEntity="Quiz\CoreBundle\Entity\Quiz", mappedBy="questions", cascade={"merge"} )
* @Exclude()
*/
protected $quizes;
/**
* @ORM\OneToMany(targetEntity="Quiz\CoreBundle\Entity\Answer", mappedBy="question", cascade={"remove", "merge", "persist"}, orphanRemoval=true)
* @Groups({"public", "admin"})
*/
protected $answers;
答案:
/**
* @ORM\ManyToOne(targetEntity="Quiz\CoreBundle\Entity\Question", inversedBy="answers", cascade={"merge", "remove"})
* @Groups({"public", "admin"})
*/
protected $question;
保存操作:
$toSave = $this->get('request')->getContent();
/* @var $s Quiz */
$s = $serializer->deserialize ($toSave, 'Quiz\CoreBundle\Entity\Quiz', 'json');
$em->merge($s);
$em->flush();
当我添加/更新问题/答案时它工作正常,但当我尝试从问题中删除问题或答案时它不起作用。有什么帮助吗?