Short Description of the problem:
I generate a file inside the entity class and would like to save the filename to the database. The controller doesn’t know about this (wheter or not the filename has changed, so it’s not practical to persist from the controller.
Is there a way for an Entity to persist itself?
Example of my use:
The entity class is for an image in a gallery. I always keep the original file and work with a cached version of the file. When the image is changed (rotated for example), the cached version is deleted. The cached version also be deleted in other cases. When the file is needed, I check if the cached file exists, otherwise it is regenerated with a new filename from the archived image. I need a new filename because that resets the cache for various thumbnail sizes.
When I generate that new file, I have to save its filename to the database somehow. Because it is only decided in the Entity when to regenerate the image, it would be practical if the entity could persist itself to the database, but I haven’t found a solution for that.
Is there a way to do this or is there a whole different concept I should be using to regenerate the image file?
Entities in Doctrine are not active records – they cannot perform persistance actions by themselves, so they rely on a Big Brother [the entity manager].
Even if the controller doesn’t know wether any filename as changed or not, you do – just persist your picture every time: if nothing changed, Doctrine won’t touch the entity.
Have a look at lifecycle events too, maybe you can find useful to fire a
@PreUpdatemethod before persistance [e.g. generating thumbnails].