学说删除必须进行两次

doctrine deleting has to be done twice

我有以下数据库结构和关系:

Posts -> OneToMany,cascade={"persist"} -> 回复 -> OneToMany,级联={"persist"} -> Files entities + files on disk -> event 删除文件实体

Posts -> OneToMany,cascade={"persist"} -> 文件实体 + 磁盘上的文件 -> 事件删除文件实体

关于文件的事情是:

我需要的是,如果 post 被删除,它会一直级联到删除回复、文件实体及其在磁盘上的文件。事件链是:

  1. 页面控制器:
post->remove();
  1. Post class:
remove()
{
    PostFile::deleteFiles($this);
    Reply::deleteFiles($this);
    $em->remove($this);
    $em->flush();
}

  1. Post文件:
deleteFiles($post)
{
    $files = $post->getFiles()->toArray();
    foreach ($files as $file) {
        $f->delete();
    }
}
  1. 回复:
deleteFiles($post)
{
    $replies = $post->getReplies()->toArray();
    foreach ($replies as $reply) {
        if ($reply) {
            ReplyFile::deleteFiles($reply);
        }
    }
}
  1. 回复文件:
deleteFiles($reply)
{
    $files = $reply->getFiles()->toArray();
    foreach ($files as $file) {
        $f->delete();
    }
}
  1. 文件删除事件:
Events::addListener('on_file_delete', function($event) {
    $f = $event->getFileObject();
    $fid = $file->getFileID();
    $fpf = PostFile::getByFileID($fid);
    $fpf->delete();
    $fpmf = ReplyFile::getByFileID($fid);
    $fpmf->delete();
});

链下的一切工作正常,例如一切都被删除,除了 post 本身没有被删除,它必须被删除两次。为什么会这样?

将 OneToMany,cascade={"persist"} 更改为 OneToMany,cascade={"remove"} 似乎已修复它,现在第一次尝试时所有内容都会被删除