Attribute.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Model\ResourceModel;
  7. use Magento\Catalog\Api\Data\ProductInterface;
  8. use Magento\Catalog\Model\Attribute\LockValidatorInterface;
  9. /**
  10. * Catalog attribute resource model
  11. *
  12. * @author Magento Core Team <core@magentocommerce.com>
  13. */
  14. class Attribute extends \Magento\Eav\Model\ResourceModel\Entity\Attribute
  15. {
  16. /**
  17. * Eav config
  18. *
  19. * @var \Magento\Eav\Model\Config
  20. */
  21. protected $_eavConfig;
  22. /**
  23. * @var LockValidatorInterface
  24. */
  25. protected $attrLockValidator;
  26. /**
  27. * @var \Magento\Framework\EntityManager\MetadataPool
  28. */
  29. protected $metadataPool;
  30. /**
  31. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  32. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  33. * @param \Magento\Eav\Model\ResourceModel\Entity\Type $eavEntityType
  34. * @param \Magento\Eav\Model\Config $eavConfig
  35. * @param LockValidatorInterface $lockValidator
  36. * @param string $connectionName
  37. */
  38. public function __construct(
  39. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  40. \Magento\Store\Model\StoreManagerInterface $storeManager,
  41. \Magento\Eav\Model\ResourceModel\Entity\Type $eavEntityType,
  42. \Magento\Eav\Model\Config $eavConfig,
  43. LockValidatorInterface $lockValidator,
  44. $connectionName = null
  45. ) {
  46. $this->attrLockValidator = $lockValidator;
  47. $this->_eavConfig = $eavConfig;
  48. parent::__construct($context, $storeManager, $eavEntityType, $connectionName);
  49. }
  50. /**
  51. * Perform actions before object save
  52. *
  53. * @param \Magento\Framework\Model\AbstractModel $object
  54. * @return $this
  55. */
  56. protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
  57. {
  58. $applyTo = $object->getApplyTo();
  59. if (is_array($applyTo)) {
  60. $object->setApplyTo(implode(',', $applyTo));
  61. }
  62. return parent::_beforeSave($object);
  63. }
  64. /**
  65. * Perform actions after object save
  66. *
  67. * @param \Magento\Framework\Model\AbstractModel $object
  68. * @return $this
  69. */
  70. protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
  71. {
  72. $this->_clearUselessAttributeValues($object);
  73. return parent::_afterSave($object);
  74. }
  75. /**
  76. * Clear useless attribute values
  77. *
  78. * @param \Magento\Framework\Model\AbstractModel $object
  79. * @return $this
  80. */
  81. protected function _clearUselessAttributeValues(\Magento\Framework\Model\AbstractModel $object)
  82. {
  83. $origData = $object->getOrigData();
  84. if ($object->isScopeGlobal() && isset(
  85. $origData['is_global']
  86. ) && \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL != $origData['is_global']
  87. ) {
  88. $attributeStoreIds = array_keys($this->_storeManager->getStores());
  89. if (!empty($attributeStoreIds)) {
  90. $delCondition = [
  91. 'attribute_id = ?' => $object->getId(),
  92. 'store_id IN(?)' => $attributeStoreIds,
  93. ];
  94. $this->getConnection()->delete($object->getBackendTable(), $delCondition);
  95. }
  96. }
  97. return $this;
  98. }
  99. /**
  100. * Delete entity
  101. *
  102. * @param \Magento\Framework\Model\AbstractModel $object
  103. * @return $this
  104. * @throws \Magento\Framework\Exception\LocalizedException
  105. */
  106. public function deleteEntity(\Magento\Framework\Model\AbstractModel $object)
  107. {
  108. if (!$object->getEntityAttributeId()) {
  109. return $this;
  110. }
  111. $result = $this->getEntityAttribute($object->getEntityAttributeId());
  112. if ($result) {
  113. $attribute = $this->_eavConfig->getAttribute(
  114. $object->getEntityTypeId(),
  115. $result['attribute_id']
  116. );
  117. try {
  118. $this->attrLockValidator->validate($attribute, $result['attribute_set_id']);
  119. } catch (\Magento\Framework\Exception\LocalizedException $exception) {
  120. throw new \Magento\Framework\Exception\LocalizedException(
  121. __('Attribute \'%1\' is locked. %2', $attribute->getAttributeCode(), $exception->getMessage())
  122. );
  123. }
  124. $backendTable = $attribute->getBackend()->getTable();
  125. if ($backendTable) {
  126. $linkField = $this->getMetadataPool()
  127. ->getMetadata(ProductInterface::class)
  128. ->getLinkField();
  129. $backendLinkField = $attribute->getBackend()->getEntityIdField();
  130. $select = $this->getConnection()->select()
  131. ->from(['b' => $backendTable])
  132. ->join(
  133. ['e' => $attribute->getEntity()->getEntityTable()],
  134. "b.$backendLinkField = e.$linkField"
  135. )->where('b.attribute_id = ?', $attribute->getId())
  136. ->where('e.attribute_set_id = ?', $result['attribute_set_id']);
  137. $this->getConnection()->query($select->deleteFromSelect('b'));
  138. }
  139. }
  140. $condition = ['entity_attribute_id = ?' => $object->getEntityAttributeId()];
  141. $this->getConnection()->delete($this->getTable('eav_entity_attribute'), $condition);
  142. return $this;
  143. }
  144. /**
  145. * @return \Magento\Framework\EntityManager\MetadataPool
  146. */
  147. private function getMetadataPool()
  148. {
  149. if (null === $this->metadataPool) {
  150. $this->metadataPool = \Magento\Framework\App\ObjectManager::getInstance()
  151. ->get(\Magento\Framework\EntityManager\MetadataPool::class);
  152. }
  153. return $this->metadataPool;
  154. }
  155. }