在Doctrine中更新实体需要花费太多时间

时间:2016-02-04 05:09:40

标签: symfony doctrine

我有以下代码更新SalesOrder实体。

public function updateAction($id)
{
    $securityContext = $this->get('security.context');
    $em = $this->getDoctrine()->getEntityManager();
    $entity = $em->getRepository('CIInventoryBundle:SalesOrder')->find($id);
    $editForm = $this->createForm(new SalesOrderType($securityContext), $entity);
    $currentTotal = $entity->getTotal();
    $currentStatus = $entity->getStatus();

    try {
        $entity->isEditable();
    } catch (\Exception $e){
        $this->get('session')->setFlash('alert-error', $e->getMessage());
        return $this->redirect($this->generateUrl('salesorder_show', array('id' => $entity->getId())));
    }

    $originalItems = array();
    foreach ($entity->getItems() as $item) {
        $originalItems[] = $item;
    }

    $request = $this->getRequest();
    $editForm->bindRequest($request);

    if ($editForm->isValid()) {  
        try {
            $entity->validateStatusChange($currentStatus, $currentTotal);
        } catch (\Exception $e) {
            $this->get('session')->setFlash('alert-error', $e->getMessage());
            return $this->redirect($this->generateUrl('salesorder_show', array('id' => $id)));
        }

        foreach ($entity->getItems() as $item) {
            //adjust the reserved in the inventory
            $item->adjustReserved();

            foreach ($originalItems as $key => $toDel) {
                if ($toDel->getId() === $item->getId()) {                   
                    unset($originalItems[$key]);
                }
            }
        }

        foreach ($originalItems as $item) {
            $em->remove($item);
        }

        $entity->setUpdatedAt(new \DateTime('now'));
        $em->persist($entity);
        $em->flush();                   

        $this->get('session')->setFlash('alert-success', 'Record has been updated successfully!');
        return $this->redirect($this->generateUrl('salesorder_show', array('id' => $id)));

    } else {
        $this->get('session')->setFlash('alert-error', 'Please fill in the correct values.');
        $variantForm   = $this->createForm(new AddVariantSalesOrderType());
    }

    return array(
        'entity'      => $entity,
        'edit_form'   => $editForm->createView(),
        'variant_form' => $variantForm->createView(),
    );
}

当我运行此代码时,我得到了#34;"超过了最大执行时间"。我已经将php.ini中的最长执行时间更改为2分钟。

无论如何我可以优化我的代码吗?

谢谢!

0 个答案:

没有答案