Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine 2: Force scheduleForUpdate on a non-changed entity

How can I schedule an entity for update, manually, when no property is actually changed?

I tried $entityManager->getUnitOfWork()->scheduleForUpdate($entity) but it gave an error in the core, and I have no intetion of debuging Doctrine.

The entity is managed if it matters: $entity = $repository->findOne(1)

I need this so doctrine would call my EventSubscriber on flush().

I've also tried something like $entityManager->getEventManager()->dispatchEvent(\Doctrine\ORM\Events::preUpdate), but then my listener's preUpdate() receives EventArgs instead of PreUpdateEventArgs.

Any help is appreciated!

like image 313
Tony Bogdanov Avatar asked Feb 18 '23 04:02

Tony Bogdanov


2 Answers

Method mentioned by Wpigott not working for me (at least in doctrine/orm v2.4.2), instead I'm using this:

$entityManager->getUnitOfWork()->setOriginalEntityProperty(spl_object_hash($entity), 'field_name', '__fake_value__');

Where field_name existent property name.

like image 59
John Avatar answered Feb 20 '23 18:02

John


The solution is a bit hacky, but I was able to achieve this by doing something like the following.

$objectManager->getUnitOfWork()->setOriginalDocumentData($object, array('__fake_field'=>'1'));

This essentially causes Doctrine to think the document has changed from the original, and it computes it as a change which will cause the events to be executed on flush.

The example is for the MongoODM solution, but the same technique should work for ORM like below.

$objectManager->getUnitOfWork()->setOriginalEntityData($object, array('__fake_field'=>'1'));
like image 21
Wpigott Avatar answered Feb 20 '23 17:02

Wpigott