开发者

Doctrine 2, Need to execute code pre-persist/post-persist

开发者 https://www.devze.com 2023-04-12 14:07 出处:网络
I am using Doctrine 2 entities. We have some entities which have to update related items when they are saved to the database. For example, when a user record is modified, we save it as a new record, w

I am using Doctrine 2 entities. We have some entities which have to update related items when they are saved to the database. For example, when a user record is modified, we save it as a new record, with the "inactive" field set to 'false'. However, we have to set the the 'inactive' field for all previous record for that user to 'true'. This is done to keep an audit history. It is a Legacy database, so changing the structure is not an option.

Since Doctrine saves objects by passing them to a persister object (persist::($thisObj)), rather than the object having a save method ($thisObj->save()), we can't just extend a 'save' method from a parent object. The only option I see here is to try to extend the 'persist' object, but that sounds like a goose gaggle开发者_运维问答, just waiting to happen.

I found some information on events, but do not see how to add them to make events fire a particular function when a particular entity is persisted.

How do I add pre-save/post-save functionality to some of my entities ?


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 entity contains callbacks and then create specific functions (which need to be public) on that entity and also annotate them with @PrePersist or @PostPersist or whatever.

The other way is creating an event subscriber, register that one with the doctrine event manager and implement methods called prePersist, postPersist etc. They get passed an EventArguments object which contains the entity relevant for the occurred event.

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

Please dont exend the entity manager and overwrite the persist method, there are way cleaner methods for doing what you need as far as I can tell.


It's actually quite simple to do what you want to do. It does not require dorking with the event manager, or anything complex like that. You use something called "Lifecycle callbacks". These are functions that Doctrine automatically runs during the "lifecycle" of the entity, ie: prePersist, postPersist, preUpdate, postUpdate, etc. You can find the complete list here: http://www.doctrine-project.org/docs/orm/2.0/en/reference/events.html

The process of adding this functionality to your entities is very simple.

  1. In the Annotations section of your entity, include the following tag: "@HasLifecycleCallbacks". This tells Doctrine that it should search the entity for functions to run upon various events
  2. Write a public function in your entity that you would like to fire upon a specific event.
  3. Put an annotation above the function indicating which event it should be used to handle.

For example, look at the following code:

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

I have found that sometimes the events simply refuse to fire, and I don't yet know why. But when they do fire, they will fire reliably.


Don't forget to enable Lifecycle Callbacks in your class annotation :

/**
 * Report\MainBundle\Entity\Serveur
 * @ORM\HasLifecycleCallbacks
 */
class Serveur {
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号