I'm using doctrine-mongodb-odm-1.0.0-BETA10
and trying to provide some custom logic based on \InitialDocument
while preUpdate
event running.
Lets say \InitialDocument
obtained some state which must behave as initial one for new \StateDocument
. I'm doing something like this:
class InitDocListener implements \Doctrine\Common\EventSubscriber {
public function getSubscribedEvents()
{
return [
Events::preUpdate
];
}
public function preUpdate($args){
$document = $args->getDocument();
if($document instanceOf InitialDocument && $document->getState() == 'mine'){
$stateDocument = new \StateDocument();
$stateDocument->setInitDocument($document);
$args->getDocumentManager()->persist($stateDocument);
//no flush cause recursion happens
}
}
}
prePersist
event by \StateDocument
happens, but it will not persist new document in db. and postPersist
event accordingly will never be fired.
There is some more custom logic but all in event scope. At some point that logic may throw an Exception which must stop update event of InitialDocument
so InitialDocument
state depends of \StateDocument
creation process at business scope.
How can I solve this problem? preFlush
event to run before changeSet recalculation do not determine InitialDocument
instance. So it is some kind of trick to "search" updates at preFlush
and cause me to think it is not proper way. Please advise me in proper one. Thanks.