Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Attribute.php
Go to the documentation of this file.
1 <?php
7 
10 
17 {
23  protected $_eavConfig;
24 
28  protected $attrLockValidator;
29 
33  protected $metadataPool;
34 
43  public function __construct(
44  \Magento\Framework\Model\ResourceModel\Db\Context $context,
46  \Magento\Eav\Model\ResourceModel\Entity\Type $eavEntityType,
47  \Magento\Eav\Model\Config $eavConfig,
48  LockValidatorInterface $lockValidator,
49  $connectionName = null
50  ) {
51  $this->attrLockValidator = $lockValidator;
52  $this->_eavConfig = $eavConfig;
53  parent::__construct($context, $storeManager, $eavEntityType, $connectionName);
54  }
55 
62  protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
63  {
64  $applyTo = $object->getApplyTo();
65  if (is_array($applyTo)) {
66  $object->setApplyTo(implode(',', $applyTo));
67  }
68  return parent::_beforeSave($object);
69  }
70 
77  protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
78  {
79  $this->_clearUselessAttributeValues($object);
80  return parent::_afterSave($object);
81  }
82 
89  protected function _clearUselessAttributeValues(\Magento\Framework\Model\AbstractModel $object)
90  {
91  $origData = $object->getOrigData();
92 
93  if ($object->isScopeGlobal() && isset(
94  $origData['is_global']
95  ) && \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL != $origData['is_global']
96  ) {
97  $attributeStoreIds = array_keys($this->_storeManager->getStores());
98  if (!empty($attributeStoreIds)) {
99  $delCondition = [
100  'attribute_id = ?' => $object->getId(),
101  'store_id IN(?)' => $attributeStoreIds,
102  ];
103  $this->getConnection()->delete($object->getBackendTable(), $delCondition);
104  }
105  }
106 
107  return $this;
108  }
109 
117  public function deleteEntity(\Magento\Framework\Model\AbstractModel $object)
118  {
119  if (!$object->getEntityAttributeId()) {
120  return $this;
121  }
122 
123  $result = $this->getEntityAttribute($object->getEntityAttributeId());
124  if ($result) {
125  $attribute = $this->_eavConfig->getAttribute(
126  $object->getEntityTypeId(),
127  $result['attribute_id']
128  );
129 
130  try {
131  $this->attrLockValidator->validate($attribute, $result['attribute_set_id']);
132  } catch (\Magento\Framework\Exception\LocalizedException $exception) {
133  throw new \Magento\Framework\Exception\LocalizedException(
134  __('Attribute \'%1\' is locked. %2', $attribute->getAttributeCode(), $exception->getMessage())
135  );
136  }
137 
138  $backendTable = $attribute->getBackend()->getTable();
139  if ($backendTable) {
140  $linkField = $this->getMetadataPool()
141  ->getMetadata(ProductInterface::class)
142  ->getLinkField();
143 
144  $backendLinkField = $attribute->getBackend()->getEntityIdField();
145 
146  $select = $this->getConnection()->select()
147  ->from(['b' => $backendTable])
148  ->join(
149  ['e' => $attribute->getEntity()->getEntityTable()],
150  "b.$backendLinkField = e.$linkField"
151  )->where('b.attribute_id = ?', $attribute->getId())
152  ->where('e.attribute_set_id = ?', $result['attribute_set_id']);
153 
154  $this->getConnection()->query($select->deleteFromSelect('b'));
155  }
156  }
157 
158  $condition = ['entity_attribute_id = ?' => $object->getEntityAttributeId()];
159  $this->getConnection()->delete($this->getTable('eav_entity_attribute'), $condition);
160 
161  return $this;
162  }
163 
167  private function getMetadataPool()
168  {
169  if (null === $this->metadataPool) {
171  ->get(\Magento\Framework\EntityManager\MetadataPool::class);
172  }
173  return $this->metadataPool;
174  }
175 }
deleteEntity(\Magento\Framework\Model\AbstractModel $object)
Definition: Attribute.php:117
__construct(\Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Eav\Model\ResourceModel\Entity\Type $eavEntityType, \Magento\Eav\Model\Config $eavConfig, LockValidatorInterface $lockValidator, $connectionName=null)
Definition: Attribute.php:43
$storeManager
__()
Definition: __.php:13
_afterSave(\Magento\Framework\Model\AbstractModel $object)
Definition: Attribute.php:77
_beforeSave(\Magento\Framework\Model\AbstractModel $object)
Definition: Attribute.php:62
_clearUselessAttributeValues(\Magento\Framework\Model\AbstractModel $object)
Definition: Attribute.php:89