2
votes

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?

1

1 Answers

1
votes

I elaborated on this further in one of your previous questions, but to reiterate from Doctrine's documentation:

  • Changes to associations of the updated entity are never allowed in this event -Changes to associations of the passed entities are not recognized by the flush operation anymore.

With the level of complexity you are trying to handle in a listener, I think you would be better off making a service that handles some of this and call that instead.