Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Update.php
Go to the documentation of this file.
1 <?php
7 
18 
22 class Update implements UpdateInterface
23 {
27  private $metadataPool;
28 
32  private $typeResolver;
33 
37  private $resourceConnection;
38 
42  private $eventManager;
43 
47  private $updateMain;
48 
52  private $updateAttributes;
53 
57  private $updateExtensions;
58 
68  public function __construct(
69  MetadataPool $metadataPool,
70  TypeResolver $typeResolver,
71  ResourceConnection $resourceConnection,
72  EventManager $eventManager,
73  UpdateMain $updateMain,
74  UpdateAttributes $updateAttributes,
75  UpdateExtensions $updateExtensions
76  ) {
77  $this->metadataPool = $metadataPool;
78  $this->typeResolver = $typeResolver;
79  $this->resourceConnection = $resourceConnection;
80  $this->eventManager = $eventManager;
81  $this->updateMain = $updateMain;
82  $this->updateAttributes = $updateAttributes;
83  $this->updateExtensions = $updateExtensions;
84  }
85 
92  public function execute($entity, $arguments = [])
93  {
94  $entityType = $this->typeResolver->resolve($entity);
95  $metadata = $this->metadataPool->getMetadata($entityType);
96  $connection = $this->resourceConnection->getConnectionByName($metadata->getEntityConnectionName());
97  $connection->beginTransaction();
98  try {
99  $this->eventManager->dispatch(
100  'entity_manager_save_before',
101  [
102  'entity_type' => $entityType,
103  'entity' => $entity
104  ]
105  );
106  $this->eventManager->dispatchEntityEvent($entityType, 'save_before', ['entity' => $entity]);
107  $entity = $this->updateMain->execute($entity, $arguments);
108  $entity = $this->updateAttributes->execute($entity, $arguments);
109  $entity = $this->updateExtensions->execute($entity, $arguments);
110  $this->eventManager->dispatchEntityEvent($entityType, 'save_after', ['entity' => $entity]);
111  $this->eventManager->dispatch(
112  'entity_manager_save_after',
113  [
114  'entity_type' => $entityType,
115  'entity' => $entity
116  ]
117  );
118  $connection->commit();
119  } catch (DuplicateException $e) {
120  $connection->rollBack();
121  throw new AlreadyExistsException(new Phrase('Unique constraint violation found'), $e);
122  } catch (\Exception $e) {
123  $connection->rollBack();
124  throw $e;
125  }
126  return $entity;
127  }
128 }
$entity
Definition: element.phtml:22
$arguments
__construct(MetadataPool $metadataPool, TypeResolver $typeResolver, ResourceConnection $resourceConnection, EventManager $eventManager, UpdateMain $updateMain, UpdateAttributes $updateAttributes, UpdateExtensions $updateExtensions)
Definition: Update.php:68
$connection
Definition: bulk.php:13