Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Create.php
Go to the documentation of this file.
1 <?php
7 
12 
17 {
21  private $metadataPool;
22 
26  private $typeResolver;
27 
31  private $resourceConnection;
32 
38  public function __construct(
39  MetadataPool $metadataPool,
40  TypeResolver $typeResolver,
41  ResourceConnection $resourceConnection
42  ) {
43  $this->metadataPool = $metadataPool;
44  $this->typeResolver = $typeResolver;
45  $this->resourceConnection = $resourceConnection;
46  }
47 
58  public function execute($entity, $arguments = [])
59  {
60  $entityType = $this->typeResolver->resolve($entity);
61  $metadata = $this->metadataPool->getMetadata($entityType);
62  $connection = $this->resourceConnection->getConnection($metadata->getEntityConnectionName());
63  try {
64  $connection->beginTransaction();
65  $data = [];
66  foreach ($entity->getItems() as $operation) {
67  $data[] = $operation->getData();
68  }
69  $connection->insertOnDuplicate(
70  $metadata->getEntityTable(),
71  $data,
72  [
73  'status',
74  'error_code',
75  'result_message',
76  ]
77  );
78  $connection->commit();
79  } catch (\Exception $e) {
80  $connection->rollBack();
81  throw $e;
82  }
83  return $entity;
84  }
85 }
__construct(MetadataPool $metadataPool, TypeResolver $typeResolver, ResourceConnection $resourceConnection)
Definition: Create.php:38
$entity
Definition: element.phtml:22
$arguments
$connection
Definition: bulk.php:13