Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UpdateRow.php
Go to the documentation of this file.
1 <?php
8 
13 
17 class UpdateRow
18 {
22  private $metadataPool;
23 
27  private $resourceConnection;
28 
35  public function __construct(
36  MetadataPool $metadataPool,
37  ResourceConnection $resourceConnection
38  ) {
39  $this->metadataPool = $metadataPool;
40  $this->resourceConnection = $resourceConnection;
41  }
42 
50  {
51  $output = [];
52  foreach ($connection->describeTable($metadata->getEntityTable()) as $column) {
53  $columnName = strtolower($column['COLUMN_NAME']);
54  if ($this->canNotSetTimeStamp($columnName, $column, $data) || $column['IDENTITY']) {
55  continue;
56  }
57 
58  if (isset($data[$columnName])) {
59  $output[strtolower($column['COLUMN_NAME'])] = $data[strtolower($column['COLUMN_NAME'])];
60  } elseif (!empty($column['NULLABLE'])) {
61  $output[strtolower($column['COLUMN_NAME'])] = null;
62  }
63  }
64  if (empty($data[$metadata->getIdentifierField()])) {
65  $output[$metadata->getIdentifierField()] = $metadata->generateIdentifier();
66  }
67 
68  return $output;
69  }
70 
80  private function prepareUpdateConditions(
81  EntityMetadataInterface $metadata,
83  $data
84  ) {
85  $conditions = [];
86 
87  $indexList = $connection->getIndexList($metadata->getEntityTable());
88  $primaryKeyName = $connection->getPrimaryKeyName($metadata->getEntityTable());
89 
90  foreach ($indexList[$primaryKeyName]['COLUMNS_LIST'] as $linkField) {
91  if (isset($data[$linkField])) {
92  $conditions[$linkField . ' = ?'] = $data[$linkField];
93  }
94  }
95 
96  return $conditions;
97  }
98 
105  private function canNotSetTimeStamp($columnName, $column, array $data)
106  {
107  return $column['DEFAULT'] == 'CURRENT_TIMESTAMP' && !isset($data[$columnName])
108  && empty($column['NULLABLE']);
109  }
110 
116  public function execute($entityType, $data)
117  {
118  $metadata = $this->metadataPool->getMetadata($entityType);
119 
120  $connection = $this->resourceConnection->getConnectionByName(
121  $metadata->getEntityConnectionName()
122  );
123 
124  $connection->update(
125  $metadata->getEntityTable(),
126  $this->prepareData($metadata, $connection, $data),
127  $this->prepareUpdateConditions($metadata, $connection, $data)
128  );
129 
130  return $data;
131  }
132 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__construct(MetadataPool $metadataPool, ResourceConnection $resourceConnection)
Definition: UpdateRow.php:35
prepareData(EntityMetadataInterface $metadata, AdapterInterface $connection, $data)
Definition: UpdateRow.php:49
$connection
Definition: bulk.php:13