/**
  * {@inheritdoc}
  */
 public function batchUpdate(MassActionInterface $massAction, IterableResultInterface $results, array $data)
 {
     $this->entityName = $massAction->getOptions()->offsetGet('entityName');
     $this->fieldName = empty($data['mass_edit_field']) ? null : $data['mass_edit_field'];
     if (empty($this->fieldName)) {
         throw new \RuntimeException("Field name was not specified with option 'mass_edit_field'");
     }
     $this->identifierName = $this->doctrineHelper->getSingleEntityIdentifierFieldName($this->entityName);
     $value = $data[$this->fieldName];
     $selectedIds = [];
     $entitiesCount = 0;
     $iteration = 0;
     $this->entityManager = $this->doctrineHelper->getEntityManager($this->entityName);
     $this->entityManager->beginTransaction();
     try {
         set_time_limit(0);
         foreach ($results as $result) {
             /** @var $result ResultRecordInterface */
             $selectedIds[] = $result->getValue($this->identifierName);
             $iteration++;
             if ($iteration % $this->batchSize == 0) {
                 $entitiesCount += $this->finishBatch($selectedIds, $value);
             }
         }
         if ($iteration % $this->batchSize > 0) {
             $entitiesCount += $this->finishBatch($selectedIds, $value);
         }
         $this->entityManager->commit();
     } catch (\Exception $e) {
         $this->entityManager->rollback();
         throw $e;
     }
     return $entitiesCount;
 }
Esempio n. 2
0
 /**
  * @param Actions\MassActionInterface $massAction
  *
  * @throws LogicException
  *
  * @return string
  */
 protected function getIdentifierField(MassActionInterface $massAction)
 {
     $identifier = $massAction->getOptions()->offsetGet('data_identifier');
     if (!$identifier) {
         throw new LogicException(sprintf('Mass action "%s" must define identifier name', $massAction->getName()));
     }
     return $identifier;
 }
 /**
  * Prepare mass action response
  *
  * @param MassActionInterface $massAction
  * @param int                 $countRemoved
  *
  * @return MassActionResponse
  */
 protected function getResponse(MassActionInterface $massAction, $countRemoved = 0)
 {
     $responseMessage = $massAction->getOptions()->offsetGetByPath('[messages][success]', $this->responseMessage);
     return new MassActionResponse(true, $this->translator->trans($responseMessage), ['count' => $countRemoved]);
 }
 /**
  * Get mass action handler from handler registry
  *
  * @param MassActionInterface $massAction
  *
  * @return MassActionHandlerInterface
  */
 protected function getMassActionHandler(MassActionInterface $massAction)
 {
     $handlerAlias = $massAction->getOptions()->offsetGet('handler');
     $handler = $this->handlerRegistry->getHandler($handlerAlias);
     return $handler;
 }
 /**
  * @param MassActionInterface $massAction
  * @param int                 $entitiesCount
  * @param string              $error
  *
  * @return MassActionResponse
  */
 protected function getResponse(MassActionInterface $massAction, $entitiesCount, $error = null)
 {
     $options = $massAction->getOptions()->toArray();
     $successful = $entitiesCount > 0 || empty($error);
     $message = $successful ? $this->translator->trans($options['success_message'], ['%items%' => $entitiesCount]) : $this->translator->trans($options['error_message'], ['%error%' => $error]);
     return new MassActionResponse($successful, $message);
 }