Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine 2 - Disable PrePersist from outside of entity

I'm trying to disable an entity event from outside of an entity in Doctrine 2. Everytime we insert a new record into our table, few file operations need to be run, which have been implemented in a method with the prePersist annotation. However I also need to run some data fixtures and skip the file operation part as part of the testing.

Basically I'm asking if it's possible to disable all prePersist events through the entity manager without changing anything in the entity.

like image 733
Tom Avatar asked Nov 30 '22 13:11

Tom


1 Answers

If you are ok with completely removing the life cycle callbacks then you can just blank out the callbacks manually. This is useful in the case of dynamic fixture generation. You simply do this:

$this->em->getClassMetadata(get_class($object))->setLifecycleCallbacks(array());

Thanks to Jeremy Mikola (@jmikola) for pointing me in the right direction.

like image 122
JimTheDev Avatar answered Dec 05 '22 23:12

JimTheDev