squareCircle
squareCircle

Reputation: 302

Doctrine update event on loaded Entity instance

I have problem with event in Doctrine , i need event on persist object that is already loaded

$obj = $em->getRepository(EntityName::class)->findById($someId);
$obj2 = clone $obj;
$obj2->property = 'some value';
$obj->property = 'some diff value';
$em->persist($obj2);
$em->persist($obj);

I register event in EntityName class on event @PrePersist and that works perfect for $obj2 but event is not fired for $obj, is there way to delete $obj from $em->UnitOfWorks and fire event for $obj.

The trick is that i must fire event before $em->flush()

Upvotes: 0

Views: 1364

Answers (1)

Jason Roman
Jason Roman

Reputation: 8276

@PrePersist only triggers when you are inserting a new entity. If you've already found an existing entity like you have above, then you will need to use the @PreUpdate event.

The Doctrine documentation contains information for all these events.

Of course, without knowing what you are attempting to do, and seeing your @PrePersist code, it's impossible to help you further. You might be able to use onFlush like Cerad suggested. You may need both prePersist and preUpdate.

Either way, merely calling $em->persist() does not guarantee that the prePersist event is going to be triggered.

Upvotes: 4

Related Questions