2014-01-16 44 views
1

後,這是我在更新前甚至廣東話閃存更新前的事件

public function preUpdate(LifecycleEventArgs $args) 
{ 
    $entity = $args->getEntity(); 

    if ($entity instanceof Order) { 
     if ($args->hasChangedField('status') && $args->getNewValue('status') == 'stock') { 
      $this->container->get('activity_logger')->writeLog($entity, 'purchase'); 
     } 
    } 

做到這一點,我有錯誤

FatalErrorException: Error: Maximum execution time of 60 seconds exceeded in /vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php line 2498

public function writeLog ($object, $comment) 
    { 

     $entity = new Stock(); 
     $entity->setCategory($object->getIsotope()->getCategory()->getId()); 
     $entity->setComment($comment); 
     $entity->setDate(new \DateTime('now')); 
     $entity->setUser($object->getUser()->getId()); 
     $entity->setChange(TRUE); 


     $this->em->persist($entity); 
     $this->em->flush(); 
} 

有存儲新實體另一種方式?

覺得不是很好的解決方案(做手工),其保存和別碰事件

$sql = "INSERT INTO table (field1, field2) VALUES ('foo', 'var')"; 
$stmt = $em->getConnection()->prepare($sql); 
$stmt->bindValue('invoice', $invoiceId); 
$result = $stmt->execute(); 

回答

0

要做到這一點我新創建的實體存儲在EventSuscriber私人陣列。然後,在數組長度大於0的情況下,在suscriber的onFlush部分,我堅持使用foreach循環。然後,在foreach循環之外,我清除數組(這對防止無限循環非常重要),最後調用flush。