Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AttributePersistor.php
Go to the documentation of this file.
1 <?php
8 
18 
23 {
27  private $attributeRepository;
28 
32  private $localeFormat;
33 
37  private $metadataPool;
38 
42  private $insert = [];
43 
47  private $update = [];
48 
52  private $delete = [];
53 
59  public function __construct(
60  FormatInterface $localeFormat,
61  AttributeRepositoryInterface $attributeRepository,
62  MetadataPool $metadataPool
63  ) {
64  $this->attributeRepository = $attributeRepository;
65  $this->metadataPool = $metadataPool;
66  $this->localeFormat = $localeFormat;
67  }
68 
76  {
77  $this->delete[$entityType][$link][$attributeCode] = null;
78  }
79 
88  {
89  $this->update[$entityType][$link][$attributeCode] = $value;
90  }
91 
100  {
101  $this->insert[$entityType][$link][$attributeCode] = $value;
102  }
103 
111  public function processDeletes($entityType, $context)
112  {
113  if (!isset($this->delete[$entityType]) || !is_array($this->delete[$entityType])) {
114  return;
115  }
116  $metadata = $this->metadataPool->getMetadata($entityType);
117  foreach ($this->delete[$entityType] as $link => $data) {
118  $attributeCodes = array_keys($data);
119  foreach ($attributeCodes as $attributeCode) {
121  $attribute = $this->attributeRepository->get($metadata->getEavEntityType(), $attributeCode);
122  $conditions = $this->buildDeleteConditions($attribute, $metadata, $context, $link);
123 
124  foreach ($conditions as $condition) {
125  $metadata->getEntityConnection()->delete(
126  $attribute->getBackend()->getTable(),
127  $condition
128  );
129  }
130  }
131  }
132  }
133 
141  public function processInserts($entityType, $context)
142  {
143  if (!isset($this->insert[$entityType]) || !is_array($this->insert[$entityType])) {
144  return;
145  }
146  $metadata = $this->metadataPool->getMetadata($entityType);
147  $insertData = $this->prepareInsertDataForMultipleSave($entityType, $context);
148 
149  foreach ($insertData as $table => $tableData) {
150  foreach ($tableData as $data) {
151  $metadata->getEntityConnection()->insertArray(
152  $table,
153  $data['columns'],
154  $data['data'],
155  \Magento\Framework\DB\Adapter\AdapterInterface::INSERT_IGNORE
156  );
157  }
158  }
159  }
160 
168  private function prepareInsertDataForMultipleSave($entityType, $context)
169  {
170  $metadata = $this->metadataPool->getMetadata($entityType);
171  $insertData = [];
172  foreach ($this->insert[$entityType] as $link => $data) {
173  foreach ($data as $attributeCode => $attributeValue) {
175  $attribute = $this->attributeRepository->get(
176  $metadata->getEavEntityType(),
178  );
179  $attributeTable = $attribute->getBackend()->getTable();
180  $conditions = $this->buildInsertConditions($attribute, $metadata, $context, $link);
181  $value = $this->prepareValue($entityType, $attributeValue, $attribute);
182 
183  foreach ($conditions as $condition) {
184  $condition['value'] = $value;
185  $columns = array_keys($condition);
186  $columnsHash = implode('', $columns);
187  $insertData[$attributeTable][$columnsHash]['columns'] = $columns;
188  $insertData[$attributeTable][$columnsHash]['data'][] = array_values($condition);
189  }
190  }
191  }
192 
193  return $insertData;
194  }
195 
203  public function processUpdates($entityType, $context)
204  {
205  if (!isset($this->update[$entityType]) || !is_array($this->update[$entityType])) {
206  return;
207  }
208  $metadata = $this->metadataPool->getMetadata($entityType);
209  foreach ($this->update[$entityType] as $link => $data) {
210  foreach ($data as $attributeCode => $attributeValue) {
212  $attribute = $this->attributeRepository->get(
213  $metadata->getEavEntityType(),
215  );
216  $conditions = $this->buildUpdateConditions($attribute, $metadata, $context, $link);
217 
218  foreach ($conditions as $condition) {
219  $metadata->getEntityConnection()->update(
220  $attribute->getBackend()->getTable(),
221  [
222  'value' => $this->prepareValue($entityType, $attributeValue, $attribute)
223  ],
224  $condition
225  );
226  }
227  }
228  }
229  }
230 
240  protected function buildUpdateConditions(
242  EntityMetadataInterface $metadata,
243  array $scopes,
244  $linkFieldValue
245  ) {
246  $condition = [
247  $metadata->getLinkField() . ' = ?' => $linkFieldValue,
248  'attribute_id = ?' => $attribute->getAttributeId(),
249  ];
250 
251  foreach ($scopes as $scope) {
252  $identifier = $metadata->getEntityConnection()->quoteIdentifier($scope->getIdentifier());
253  $condition[$identifier . ' = ?'] = $this->getScopeValue($scope, $attribute);
254  }
255 
256  return [
257  $condition,
258  ];
259  }
260 
270  protected function buildDeleteConditions(
272  EntityMetadataInterface $metadata,
273  array $scopes,
274  $linkFieldValue
275  ) {
276  $condition = [
277  $metadata->getLinkField() . ' = ?' => $linkFieldValue,
278  'attribute_id = ?' => $attribute->getAttributeId(),
279  ];
280 
281  foreach ($scopes as $scope) {
282  $identifier = $metadata->getEntityConnection()->quoteIdentifier($scope->getIdentifier());
283  $condition[$identifier . ' = ?'] = $this->getScopeValue($scope, $attribute);
284  }
285 
286  return [
287  $condition,
288  ];
289  }
290 
300  protected function buildInsertConditions(
302  EntityMetadataInterface $metadata,
303  array $scopes,
304  $linkFieldValue
305  ) {
306  $condition = [
307  $metadata->getLinkField() => $linkFieldValue,
308  'attribute_id' => $attribute->getAttributeId(),
309  ];
310 
311  foreach ($scopes as $scope) {
312  $condition[$scope->getIdentifier()] = $this->getScopeValue($scope, $attribute);
313  }
314 
315  return [
316  $condition,
317  ];
318  }
319 
327  public function flush($entityType, $context)
328  {
329  $this->processDeletes($entityType, $context);
330  $this->processInserts($entityType, $context);
331  $this->processUpdates($entityType, $context);
332  unset($this->delete, $this->insert, $this->update);
333  }
334 
343  {
344  $metadata = $this->metadataPool->getMetadata($entityType);
345  $type = $attribute->getBackendType();
346  if (($type == 'int' || $type == 'decimal' || $type == 'datetime') && $value === '') {
347  $value = null;
348  } elseif ($type == 'decimal') {
349  $value = $this->localeFormat->getNumber($value);
350  } elseif ($type == 'varchar' && is_array($value)) {
351  $value = implode(',', $value);
352  }
353  $describe = $metadata->getEntityConnection()->describeTable($attribute->getBackendTable());
354  return $metadata->getEntityConnection()->prepareColumnValue($describe['value'], $value);
355  }
356 
364  protected function getScopeValue(ScopeInterface $scope, AbstractAttribute $attribute, $useDefault = false)
365  {
366  if ($useDefault && $scope->getFallback()) {
367  return $this->getScopeValue($scope->getFallback(), $attribute, $useDefault);
368  }
369  return $scope->getValue();
370  }
371 }
buildDeleteConditions(AbstractAttribute $attribute, EntityMetadataInterface $metadata, array $scopes, $linkFieldValue)
registerUpdate($entityType, $link, $attributeCode, $value)
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
buildUpdateConditions(AbstractAttribute $attribute, EntityMetadataInterface $metadata, array $scopes, $linkFieldValue)
__construct(FormatInterface $localeFormat, AttributeRepositoryInterface $attributeRepository, MetadataPool $metadataPool)
$columns
Definition: default.phtml:15
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16
$attributeCode
Definition: extend.phtml:12
getScopeValue(ScopeInterface $scope, AbstractAttribute $attribute, $useDefault=false)
prepareValue($entityType, $value, AbstractAttribute $attribute)
registerInsert($entityType, $link, $attributeCode, $value)
$table
Definition: trigger.php:14
registerDelete($entityType, $link, $attributeCode)
buildInsertConditions(AbstractAttribute $attribute, EntityMetadataInterface $metadata, array $scopes, $linkFieldValue)