Saving Changes Made to a Document Using the Doctrine PreUpdate Life Cycle Event

I have an event subscriber that contains two lifecycle event methods: prePersist and preUpdate. PrePersist works as expected - I modify the document and the changes are reflected later when I retrieve the document. However, preUpdate does not work as expected. Here's basically what it looks like:

/**
 * Also update the foo code when the bar profile is updated.
 * @param LifecycleEventArgs $args
 */
public function preUpdate(LifecycleEventArgs $args)
{
    $document = $args->getDocument();
    if ($document instanceof BarProfile) {
        $document->setFooCode('Salamanders');
    }
}

If I create a document and set its fooCode to "placeholder" in the perPersist function, then when I get the document later, fooCode will be "placeholder". If I then update it and load it again, then I expect its fooCode to be β€œSalamanders”. However, it is still a placeholder. I even tried adding error_log()material there , and it writes the material to the logs, so I see that this method is being executed.

Is there a second step that I have to do after $document->setFooCode()to create a new fooCode value?

+4
source share
1 answer

preUpdate, . . :

$eventArgs->setNewValue('fooCode', 'Salamanders');

: "PrePersist - , , ".

, persist update. , . , , Doctrine. , persist() , flush(). :

// inserts a new entity into the database
$document = new Document();
$document->setName('My Document');

$em->persist($document);
$em->flush();

// retrieves entity from the database, makes a change, then updates the database
$document = $em->findOneByName('My Document');
$document->setFooCode('Salamanders');

$em->flush();

Doctrine, Cerad. preUpdate:

  • PreUpdate
  • flush, , ,
+2

Source: https://habr.com/ru/post/1569492/


All Articles