I have a Restaurant entity with a One-To-One relationship with an ImageRestaurant entity.
I can edit both entities using a single form (ImageRestauranType is embedded in RestaurantType).
Restaurant entity has a slug, which is generated thanks to the StofDoctrineExtensionsBundle, and using the Gedmo\Sluggable annotation.
I am trying to rename the file linked to my ImageRestaurant entity using the slug attribute (slug.jpg for example) each time a Restaurant entity is updated and the slug attribute is regenerated. I want also to change the name attribute of the ImageRestaurant entity.
I have two options using the Doctrine events :
- onFlush event :
I have access to the entities scheduled for update (Restaurant) but at this time, the updated slug is not accessible. So I can not update the file and the ImageRestaurant entity.
- postUpdate event :
I have access to the updated slug but as described in the documentation this event is not relevant to persistence in database :
The three post events are called inside EntityManager#flush(). Changes in here are not relevant to the persistence in the database, but you can use these events to alter non-persistable items, like non-mapped fields, logging or even associated classes that are not directly mapped by Doctrine.
For this event, as I have access to the entities ID I thought I could fetch both entities Restaurant (the frehsly updated one) and ImageRestaurant from database and proceed to file and ImageRestaurant update ?
Any ideas on how I could achieve this ?