Tom Tom - 1 year ago 121
PHP Question

Doctrine 2 PrePersist doesn't fire

Within the same entity I have a PreUpdate and a PrePersist. The PreUpdate fires, but the PrePersist never does. I put a

after the flush and comments within the lifecycle callbacks. Full entity can be seen at

Entity callbacks

* @PreUpdate
public function fixDates(){
$this->updatedOn = $this->getNow();
$this->closedDate = null;
$this->openDate = null;
print "dates fixed";

* @PrePersist
public function prePersist() {
print 'in prePersist';

Entity Manager calls



My screen reads "dates fixed", but not the prePersist message. I do have the
at the top of the entity.

Answer Source

PrePersist is fired only when you are performing INSERT statement, not UPDATE statement.

When testing, don't forget that the UPDATE statement is fired only when the entity attributes really change. If the Entity Manager is being called to persist that entity, it first looks if there are any changes. If not, no sql query is performed and no @PreUpdate method is called.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download