Doctrine 2, you need to execute the pre-persist / post-persist code

I use Doctrine 2 objects. We have several objects that need to update related items when they are stored in the database. For example, when a user record is changed, we save it as a new record, and the "inactive" field is set to "false". However, we must set the field "inactive" for all previous entries for this user to "true". This is done to preserve the audit history. This is a Legacy database, so restructuring is not an option.

Since Doctrine saves objects by passing them to the persister object (persist :: ($ thisObj)), rather than an object that has a save method ( $thisObj->save() ), we cannot just extend the save method from the parent object. The only option I see here is to try expanding the persist object, but it looks like a goose flock that just waits.

I found some information about events, but I don’t see how to add them so that events light up with a certain function when a specific entity is saved.

How to add pre-save / post-save features to some of my entities?

+6
source share
3 answers

So, you probably already know http://www.doctrine-project.org/docs/orm/2.1/en/reference/events.html right?

You add an annotation that the object contains callbacks, and then creates certain functions (which should be publicly available) for this object, and also annotate them using @PrePersist or @PostPersist or something else.

Another way is to create an event subscriber, register it using the doctrine's event manager, and implement methods called prePersist, postPersist, etc. They receive the passed EventArguments object, which contains the entity that is relevant to the event.

I know this is a very general answer to your question, but you need to be more specific where your problem is.

Please do not remove the entity manager and overwrite the persist method, there are ways to use cleaner methods to accomplish what you need, as far as I can tell.

+12
source

It is actually quite simple to do what you want to do. This does not require a detachment with an event manager or something complicated. You are using something called Lifecycle Callbacks. These are functions that Doctrine automatically runs during the "life cycle" of an object, i.e. PrePersist, postPersist, preUpdate, postUpdate, etc. Here you can find the full list: http://www.doctrine-project.org/docs/orm/2.0/en/reference/events.html

The process of adding this function to your objects is very simple.

  • In the Annotations section of your entity, include the following tag: "@HasLifecycleCallbacks". This tells Doctrine that it should look for an object for functions that will execute on various events.
  • Write a public function in your organization that you would like to run with a specific event.
  • Place an annotation above the function indicating which event it should be used for processing.

For example, look at the following code:

 /** @PostPersist */ public function doSPostPersist() { $this->tester = 'Value changed by post-persist'; } 

I found that sometimes events simply refuse to shoot, and I still don't know why. But when they shoot, they will shoot reliably.

+7
source

Remember to include lifecycle callbacks in class annotations:

 /** * Report\MainBundle\Entity\Serveur * @ORM\HasLifecycleCallbacks */ class Serveur { 
+2
source

Source: https://habr.com/ru/post/899080/


All Articles