Doctrine orm Doctrine2:更新与ArrayCollection的一对多关联不会删除项
使用Doctrine2,注释实体与文件实体具有一对多关联。因此,一张便笺可以有许多与之关联的文件Doctrine orm Doctrine2:更新与ArrayCollection的一对多关联不会删除项,doctrine-orm,Doctrine Orm,使用Doctrine2,注释实体与文件实体具有一对多关联。因此,一张便笺可以有许多与之关联的文件 然后,考虑下面的代码: /* * @entityManager : this is a pre-obtained EntityManager object * @notes : expects a Note entity * @filesId : expects an array of integer representing file ids */ function setNoteFile
然后,考虑下面的代码:
/*
* @entityManager : this is a pre-obtained EntityManager object
* @notes : expects a Note entity
* @filesId : expects an array of integer representing file ids
*/
function setNoteFiles($entityManager, $note, $filesId) {
$files = new ArrayCollection();
foreach ($filesId as $fileId) {
$file = $entityManager->find('File', $fileId);
$file->setNote($note);
$files->add($file);
}
$note->setFiles($files);
$entityManager->flush();
}
因此,当我想将文件添加到便笺中时,例如使用以下命令,它会像预期的那样完美工作:
setNoteFiles($entityManager, $note, Array(1, 2, 3, 4));
但是,如果以后我想删除集合中的一个文件,当然不使用removeElement!我想设置ArrayCollection本身,如下所示:
setNoteFiles($entityManager, $note, Array(1, 2, 4));
那么,这是行不通的。在数据库中,该文件记录的外键列不会被更新为null或zero
难道条令不应该管理新的ArrayCollection来添加和删除不再需要的项吗