PreUpdate entity symfony LifecycleCallbacks

丶灬走出姿态 提交于 2019-12-02 19:11:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!