Esempio n. 1
0
 /**
  * @param string $entityType
  * @param array $entityData
  * @param array $arguments
  * @return array
  * @throws \Exception
  * @throws \Magento\Framework\Exception\ConfigurationMismatchException
  * @throws \Magento\Framework\Exception\LocalizedException
  * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  */
 public function execute($entityType, $entityData, $arguments = [])
 {
     $metadata = $this->metadataPool->getMetadata($entityType);
     if (!$metadata->getEavEntityType()) {
         //todo hasCustomAttributes
         return $entityData;
     }
     $context = $this->scopeResolver->getEntityContext($entityType, $entityData);
     $connection = $metadata->getEntityConnection();
     /** @var \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute */
     $attributeTables = [];
     $attributesMap = [];
     $selects = [];
     foreach ($this->getAttributes($entityType) as $attribute) {
         if (!$attribute->isStatic()) {
             $attributeTables[$attribute->getBackend()->getTable()][] = $attribute->getAttributeId();
             $attributesMap[$attribute->getAttributeId()] = $attribute->getAttributeCode();
         }
     }
     foreach ($attributeTables as $attributeTable => $attributeCodes) {
         $select = $connection->select()->from(['t' => $attributeTable], ['value' => 't.value', 'attribute_id' => 't.attribute_id'])->where($metadata->getLinkField() . ' = ?', $entityData[$metadata->getLinkField()]);
         foreach ($context as $scope) {
             //TODO: if (in table exists context field)
             $select->where($metadata->getEntityConnection()->quoteIdentifier($scope->getIdentifier()) . ' IN (?)', $this->getContextVariables($scope))->order('t.' . $scope->getIdentifier() . ' DESC');
         }
         $selects[] = $select;
     }
     $unionSelect = new \Magento\Framework\DB\Sql\UnionExpression($selects, \Magento\Framework\DB\Select::SQL_UNION_ALL);
     foreach ($connection->fetchAll($unionSelect) as $attributeValue) {
         $entityData[$attributesMap[$attributeValue['attribute_id']]] = $attributeValue['value'];
     }
     return $entityData;
 }
Esempio n. 2
0
 /**
  * @param string $entityType
  * @param array $entityData
  * @param array $arguments
  * @return array
  * @throws \Exception
  * @throws \Magento\Framework\Exception\ConfigurationMismatchException
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function execute($entityType, $entityData, $arguments = [])
 {
     $metadata = $this->metadataPool->getMetadata($entityType);
     if ($metadata->getEavEntityType()) {
         $processed = [];
         /** @var \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute */
         foreach ($this->getAttributes($entityType) as $attribute) {
             if ($attribute->isStatic()) {
                 continue;
             }
             if (isset($entityData[$attribute->getAttributeCode()]) && !is_array($entityData[$attribute->getAttributeCode()]) && !$attribute->isValueEmpty($entityData[$attribute->getAttributeCode()])) {
                 $entityLinkField = $metadata->getLinkField();
                 $this->attributePersistor->registerInsert($entityType, $entityData[$entityLinkField], $attribute->getAttributeCode(), $entityData[$attribute->getAttributeCode()]);
                 $processed[$attribute->getAttributeCode()] = $entityData[$attribute->getAttributeCode()];
             }
         }
         $context = $this->scopeResolver->getEntityContext($entityType, $entityData);
         $this->attributePersistor->flush($entityType, $context);
     }
     return $entityData;
 }
Esempio n. 3
0
 /**
  * @param string $entityType
  * @param array $entityData
  * @param array $arguments
  * @return array
  * @throws \Exception
  * @throws \Magento\Framework\Exception\ConfigurationMismatchException
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function execute($entityType, $entityData, $arguments = [])
 {
     /** @var \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute */
     $metadata = $this->metadataPool->getMetadata($entityType);
     if ($metadata->getEavEntityType()) {
         $context = $this->scopeResolver->getEntityContext($entityType, $entityData);
         $entityDataForSnapshot = [$metadata->getLinkField() => $entityData[$metadata->getLinkField()]];
         foreach ($context as $scope) {
             if (isset($entityData[$scope->getIdentifier()])) {
                 $entityDataForSnapshot[$scope->getIdentifier()] = $entityData[$scope->getIdentifier()];
             }
         }
         $snapshot = $this->readSnapshot->execute($entityType, $entityDataForSnapshot);
         foreach ($this->getAttributes($entityType) as $attribute) {
             if ($attribute->isStatic()) {
                 continue;
             }
             /**
              * Only scalar values can be stored in generic tables
              */
             if (isset($entityData[$attribute->getAttributeCode()]) && !is_scalar($entityData[$attribute->getAttributeCode()])) {
                 continue;
             }
             if (isset($snapshot[$attribute->getAttributeCode()]) && $snapshot[$attribute->getAttributeCode()] !== false && (array_key_exists($attribute->getAttributeCode(), $entityData) && $attribute->isValueEmpty($entityData[$attribute->getAttributeCode()]))) {
                 $this->attributePersistor->registerDelete($entityType, $entityData[$metadata->getLinkField()], $attribute->getAttributeCode());
             }
             if ((!array_key_exists($attribute->getAttributeCode(), $snapshot) || $snapshot[$attribute->getAttributeCode()] === false) && array_key_exists($attribute->getAttributeCode(), $entityData) && !$attribute->isValueEmpty($entityData[$attribute->getAttributeCode()])) {
                 $this->attributePersistor->registerInsert($entityType, $entityData[$metadata->getLinkField()], $attribute->getAttributeCode(), $entityData[$attribute->getAttributeCode()]);
             }
             if (array_key_exists($attribute->getAttributeCode(), $snapshot) && $snapshot[$attribute->getAttributeCode()] !== false && array_key_exists($attribute->getAttributeCode(), $entityData) && $snapshot[$attribute->getAttributeCode()] != $entityData[$attribute->getAttributeCode()] && !$attribute->isValueEmpty($entityData[$attribute->getAttributeCode()])) {
                 $this->attributePersistor->registerUpdate($entityType, $entityData[$metadata->getLinkField()], $attribute->getAttributeCode(), $entityData[$attribute->getAttributeCode()]);
             }
         }
         $this->attributePersistor->flush($entityType, $context);
     }
     return $this->getReadHandler()->execute($entityType, $entityData, $arguments);
 }