PreUpdate entity symfony LifecycleCallbacks

2019-03-06 19:53发布

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!!!

2条回答
你好瞎i
2楼-- · 2019-03-06 20:22

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.

查看更多
对你真心纯属浪费
3楼-- · 2019-03-06 20:36

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)
    ));
查看更多
登录 后发表回答