Dalibor
Dalibor

Reputation: 167

doctrine2 get info about updated field

HI, I want somehow to figure out which field was updated in the controller, I didn't found anything that might sound like what I'm trying to achieve in API doc for UnitOfWork. For example, I want to add on @PreUpdate code similar to this

/** @PreUpdate */ 
function updateAllIsDefaultFields(){
    //only if $this->isDefault propery for this entity is changed to 1 from its previous state, then update all other entities so they all have isDefault to 0

//else, means that this field is not changed, don't do anything

    }

Or this must be done from the controller on every action?

I know that I can do it every time just if $this->isDefault is set to 1, but I would like to avoid it for performance and I might find it useful for some other scenarios as well. Thanks in advance

Upvotes: 1

Views: 1504

Answers (2)

Cobby
Cobby

Reputation: 5454

You will have to write an Event Subscriber. Probably using an onFlush event, you can retrieve an Entities ChangeSet from the UnitOfWork to determine if a certain field has been updated and then do what ever you want.

Upvotes: 1

Koc
Koc

Reputation: 2375

There are some good behaviors for Doctrine2. Here is example of similar to your case.

Upvotes: 1

Related Questions