私は2つのモデルを持っています:
スレッド <-OneToMany-> 投稿
両側を「所有」する方法はありますか?そうでない場合、これを行うための好ましい方法は何ですか?
基本的に、私はこのシナリオが欲しいです:
// When called:
$post->setThread($thread);
$documentManager->persist($post);
// This gets called too, implicitly
$thread->addPost($post);
$documentManager->persist($thread);
// But also, when I call
$thread->addPost($post);
$documentManager->persist($thread);
// This gets called implicitly
$post->setThread($thread);
$documentManager->persist($post);
// When I remove post from thread
$thread->removePost($post);
$documentManager->persist($thread);
// This gets called implicitly
$post->setThread(null);
$documentManager->persist($post);
Etc.
ご覧のとおり、永続性と更新のカスケードを備えた双方向の同期が必要です。
モデル自体でこの動作を指定して同期を維持することもできますが、問題があります: 反対側が編集されたエンティティである場合、Doctrine は所有エンティティを自動的に保存しません。
私は Doctrine Mongo ODM を使用していますが、ORM と大した違いはないのではないでしょうか?