doctrine 2 - 从arraycollection中删除所有元素不会调用任何Event

时间:2014-06-30 17:00:35

标签: symfony events doctrine-orm arraycollection

我需要在清除arraycollection时调用事件(没有元素),然后调用pre / postUpdate。只有在我更改了arraycollection的内容并且在更改pre / postUpdate事件之后至少有一个元素被调用。

任何想法如何使其发挥作用?

1 个答案:

答案 0 :(得分:0)

public function postUpdate(LifecycleEventArgs $args)
{
    $entity = $args->getEntity();
    $em     = $args->getEntityManager();

    if ($entity instanceof Track) {
        // remove all old statuses
        $query = $em->createQuery(
            'SELECT os
             FROM CuculoERPBundle:OrderStatus os
             WHERE os.track = :track'
        )
            ->setParameter(':track', $entity->getId());

        $orderStatusArray = $query->getResult();

        foreach ($orderStatusArray as $orderStatus) {
            $em->remove($orderStatus);
        }

        // add new statuses
        foreach ($entity->getOrders() as $order) {
            var_dump($order->getId());
            $orderStatus = new OrderStatus();
            $orderStatus->setOrder($order);
            $orderStatus->setTrack($entity);
            $orderStatus->setStep($em->getReference('Cuculo\ERPBundle\Entity\OrderStep', 6));

            $em->persist($orderStatus);
        }

        $this->needsFlush = true;
    }//end if
}//end postUpdate()

我设法通过在//删除所有旧状态到UpdateAction并将条件添加到count ArrayCollection并且如果0执行此代码后复制代码来实现此目的。