Symfony2: File Upload via Doctrine does not fire the PrePersist/PreUpdate lifecycle-event

doctrine-ormsymfony

i tried to implement the file upload via doctrine/lifecycle callbacks as described here:

http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html#using-lifecycle-callbacks

So far it works, but the PrePersist/PreUpdate Event is not fired, the function "preUpload" is not called.
Functions like "upload" and "removeUpload" triggered by other lifecycle events are called correctly.

Does anyone have an idea why the event is not fired or a solution for this problem?

Thanks

Best Answer

I have another solution to this problem:

My entity has a field "updatedAt" which is a timestamp of the last update. Since this field gets set anyway (by the timestampable extension of Gedmo) I just use this field to trick doctrine into believing that the entitiy was updated. Before I persist the entity I set this field manually doing

if( $editForm['file']->getData() )
    $entity->setUpdateAt(new \DateTime());

This way the entity gets persisted (because it has changed) and the preUpdate and postUpdate functions are called properly. Of course this only works if your entity has a field that you can exploit like that.

Related Topic