I have an event subscriber for DocumentA. DocumentA has associated documents of type DocumentB. During the preUpdate lifecycle event hook for DocumentA, I'd like to refresh a value on its DocumentB. I have code like so:
public function preUpdate(LifecycleEventArgs $args)
{
$document = $args->getDocument();
if (!($document instanceof DocumentA) ||
return;
}
if ($documentsB = $document->getDocumentB()) {
$dm = $args->getDocumentManager();
foreach (iterator_to_array($documentsB) as $docB) {
$documentB = $dm->find(DocumentB::class, $docB->getId());
$documentB->setFooCode();
$dm->merge($documentB);
}
}
}
I've tried this with $dm->persist($documentB) instead of using merge(), I've set DocumentA's relationship to DocumentB to cascade: {all}, and I've tried $dm->getUnitOfWork()->recomputeSingleDocumentChangeSet($class, $document); for both DocumentA and each DocumentB, but I don't seem to be getting anywhere. I don't seem to be able to call flush() even for a single DocumentB without causing a segfault (I'm assuming it triggers an infinite loop of preUpdate events inside preUpdate events?)
How do I save the changes to my associated documents when the changes are made in the preUpdate method of DocumentA's event subscriber?