pop_up
pop_up

Reputation: 1579

need explanation on postPersist and data flushed

i had trouble with an entity who was not flushed correctly.

In a service, i setted somme values. before flushing them in the service, i call another service and i saw there was a listener linked. In this listener, there was a postPersist method in witch was called "$entityManager->flush();"

It was the source of my problem.

I found this post : Doctrine inserting in postPersist event

So, i deleted the flush who was done in the postPersist.

But i don't understand the need of the method postFlush.

In my case, data is flushed even if i don't have this method. how is it possible that the properties setted in the listener are flushed correctly without this event ?

If i look other entry points, i see that i need to declare the postFlush event and i see the need of this method.

thanks for your help

Upvotes: 0

Views: 1184

Answers (1)

hoover_D
hoover_D

Reputation: 650

No, you don't need to flush in a postPersist event, because it will be done soon, just after Persist. You don't need to use all the functions of the list, neither declaring them.

ps.: Only if you need to get/set data before persist/flush. You would need to compute changes then get them in the action 'couple' (e.g prePersist and postPersist, preUpdate and postUpdate). docs says:

Changes to fields of the passed entities are not recognized by the flush operation anymore, use the computed change-set passed to the event to modify primitive field values.

and

getEntityChangeSet() to get a copy of the changeset array. Changes to this returned array do not affect updating.

PostFlush

The postFlush is made at the end of a flush operation. According to the docs this event is not a lifecycle callback. You can use it to set something after registering, or even send notifications, clearly with postFlush you won't need to worry about lifeCycle events.

postFlush - The postFlush event occurs at the end of a flush operation. This event is not a lifecycle callback.

For postPersist in the docs

postPersist - The postPersist event occurs for an entity after the entity has been made persistent. It will be invoked after the database insert operations. Generated primary key values are available in the postPersist event.

Here you can have ids if you need before flush.

You can check the docs about LifeCycleEvents here: https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#lifecycle-events

Upvotes: 1

Related Questions