问题
I have a little problem with the PreUpdate LifecycleCallbacks in Symfony.
I have an entity User with a OneToMany relation with an entity product.
class User{
/**
* @ORM\OneToMany(targetEntity="Product", mappedBy="formulario", cascade={"persist", "remove"})
*/
private $products;
}
class Product{
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="products")
* @ORM\JoinColumn(name="user", referencedColumnName="id")
*/
private $user;
}
My problem is when I add or remove a product from the User. When this hapends I want to launch a PreUpdate function to make some changes in the User Entity. But the PreUpdate is not fire when changing the entity Product from the User.
Thanks a lot!!!
回答1:
Changing related entities is not allowed using a preUpdate listener.
Changes to associations of the updated entity are never allowed in this event, since Doctrine cannot guarantee to correctly handle referential integrity at this point of the flush operation.
... from the documentation.
回答2:
I have got same issue and I have solved it by update $user in preUpdate()
then schedule an extra update:
$args->getEntityManager()->getUnitOfWork()->scheduleExtraUpdate($user, array(
'field_name' => array($oldValue, $newValue)
));
来源:https://stackoverflow.com/questions/17808437/preupdate-entity-symfony-lifecyclecallbacks