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 
18 {
27  abstract protected function _getEavWebsiteTable();
28 
36  abstract protected function _getFormAttributeTable();
37 
44  protected function _beforeSave(AbstractModel $object)
45  {
46  $validateRules = $object->getData('validate_rules');
47  if (is_array($validateRules)) {
48  $object->setData('validate_rules', $this->getSerializer()->serialize($validateRules));
49  }
50  return parent::_beforeSave($object);
51  }
52 
61  protected function _getLoadSelect($field, $value, $object)
62  {
63  $select = parent::_getLoadSelect($field, $value, $object);
64  $websiteId = (int)$object->getWebsite()->getId();
65  if ($websiteId) {
66  $connection = $this->getConnection();
67  $columns = [];
68  $scopeTable = $this->_getEavWebsiteTable();
69  $describe = $connection->describeTable($scopeTable);
70  unset($describe['attribute_id']);
71  foreach (array_keys($describe) as $columnName) {
72  $columns['scope_' . $columnName] = $columnName;
73  }
74  $conditionSql = $connection->quoteInto(
75  $this->getMainTable() . '.attribute_id = scope_table.attribute_id AND scope_table.website_id =?',
77  );
78  $select->joinLeft(['scope_table' => $scopeTable], $conditionSql, $columns);
79  }
80 
81  return $select;
82  }
83 
91  protected function _afterSave(AbstractModel $object)
92  {
93  $forms = $object->getData('used_in_forms');
94  $connection = $this->getConnection();
95  if (is_array($forms)) {
96  $where = ['attribute_id=?' => $object->getId()];
97  $connection->delete($this->_getFormAttributeTable(), $where);
98 
99  $data = [];
100  foreach ($forms as $formCode) {
101  $data[] = ['form_code' => $formCode, 'attribute_id' => (int)$object->getId()];
102  }
103 
104  if ($data) {
105  $connection->insertMultiple($this->_getFormAttributeTable(), $data);
106  }
107  }
108 
109  // update sort order
110  if (!$object->isObjectNew() && $object->dataHasChangedFor('sort_order')) {
111  $data = ['sort_order' => $object->getSortOrder()];
112  $where = ['attribute_id=?' => (int)$object->getId()];
113  $connection->update($this->getTable('eav_entity_attribute'), $data, $where);
114  }
115 
116  // save scope attributes
117  $websiteId = (int)$object->getWebsite()->getId();
118  if ($websiteId) {
119  $table = $this->_getEavWebsiteTable();
120  $describe = $this->getConnection()->describeTable($table);
121  $data = [];
122  if (!$object->getScopeWebsiteId() || $object->getScopeWebsiteId() != $websiteId) {
123  $data = $this->getScopeValues($object);
124  }
125 
126  $data['attribute_id'] = (int)$object->getId();
127  $data['website_id'] = (int)$websiteId;
128  unset($describe['attribute_id']);
129  unset($describe['website_id']);
130 
131  $updateColumns = [];
132  foreach (array_keys($describe) as $columnName) {
133  $data[$columnName] = $object->getData('scope_' . $columnName);
134  $updateColumns[] = $columnName;
135  }
136 
137  $connection->insertOnDuplicate($table, $data, $updateColumns);
138  }
139 
140  return parent::_afterSave($object);
141  }
142 
149  public function getScopeValues(\Magento\Eav\Model\Attribute $object)
150  {
151  $connection = $this->getConnection();
152  $bind = ['attribute_id' => (int)$object->getId(), 'website_id' => (int)$object->getWebsite()->getId()];
153  $select = $connection->select()->from(
154  $this->_getEavWebsiteTable()
155  )->where(
156  'attribute_id = :attribute_id'
157  )->where(
158  'website_id = :website_id'
159  )->limit(
160  1
161  );
162  $result = $connection->fetchRow($select, $bind);
163 
164  if (!$result) {
165  $result = [];
166  }
167 
168  return $result;
169  }
170 
177  public function getUsedInForms(AbstractModel $object)
178  {
179  $connection = $this->getConnection();
180  $bind = ['attribute_id' => (int)$object->getId()];
181  $select = $connection->select()->from(
182  $this->_getFormAttributeTable(),
183  'form_code'
184  )->where(
185  'attribute_id = :attribute_id'
186  );
187 
188  return $connection->fetchCol($select, $bind);
189  }
190 }
getData($key='', $index=null)
Definition: DataObject.php:119
getUsedInForms(AbstractModel $object)
Definition: Attribute.php:177
$columns
Definition: default.phtml:15
_getLoadSelect($field, $value, $object)
Definition: Attribute.php:61
$value
Definition: gender.phtml:16
_beforeSave(AbstractModel $object)
Definition: Attribute.php:44
getScopeValues(\Magento\Eav\Model\Attribute $object)
Definition: Attribute.php:149
$connection
Definition: bulk.php:13
$table
Definition: trigger.php:14