简体   繁体   中英

Doctrine PreUpdate never worked

I have searched, and there is a lot of questions wiith the same problem, but none of them solves to my issue.

I have an Entity, here is it's code:

/*
 * @ORM\HasLifecycleCallbacks
 */
class MyEntity
{
    // some preoperties here...

    /**
     * @ORM\Column(type="text", nullable=true)
     * @Assert\MaxLength(limit="500")
     */
    private $delivery = null;

    /**
     * @var $deliveryOn bool
     *
     * Virtual field used for $delivery property organization
     */
    private $deliveryOn = false;

    /**
     * @ORM\PrePersist()
     * @ORM\PreUpdate()
     */
    public function preSetDelivery()
    {
        if ($this->deliveryOn == false)
            $this->delivery = null;
    }

    /**
     * @ORM\PostLoad()
     */
    public function loadDeliveryOn()
    {
        if ($this->delivery != null)
            $this->deliveryOn = true;
    }
}

loadDeliveryOn method perfectly works all the time. But the preSetDelivery fired only when I persist the entity to the database for the first time. I want it to be called when object is updated too, but it doesn't work. And I have no any idea why.

My edit controller:

public function editAction($id)
{
    // some code here...
    // ...

        $request = $this->getRequest();
        if ($request->isMethod('POST'))
        {
            $form->bind($request);

            if ($form->isValid())
            {
                $em->flush();
            }
        }
}

From official docs concerning preUpdate :

Changes to fields of the passed entities are not recognized by the flush operation anymore, use the computed change-set passed to the event to modify primitive field values.

If you have access to UnitOfWork maybe there is a way to recompute change-set as there is in onFlush ?

如果您使用继承-例如@ORM \\ InheritanceType(“ SINGLE_TABLE”),则需要在父类中添加@ORM \\ MappedSuperclass

PreUpdate only fires if there are actual changes on the entity. If you don't change anything in the form, there will be no changes on the entity, and no preUpdate listeners will be called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM