Attribute.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Model\ResourceModel;
  7. use Magento\Framework\DB\Select;
  8. use Magento\Framework\Model\AbstractModel;
  9. /**
  10. * EAV attribute resource model (Using Forms)
  11. *
  12. * @api
  13. * @since 100.0.2
  14. */
  15. abstract class Attribute extends \Magento\Eav\Model\ResourceModel\Entity\Attribute
  16. {
  17. /**
  18. * Get EAV website table
  19. *
  20. * Get table, where website-dependent attribute parameters are stored
  21. * If realization doesn't demand this functionality, let this function just return null
  22. *
  23. * @return string|null
  24. */
  25. abstract protected function _getEavWebsiteTable();
  26. /**
  27. * Get Form attribute table
  28. *
  29. * Get table, where dependency between form name and attribute ids are stored
  30. *
  31. * @return string|null
  32. */
  33. abstract protected function _getFormAttributeTable();
  34. /**
  35. * Perform actions before object save
  36. *
  37. * @param \Magento\Framework\Model\AbstractModel $object
  38. * @return $this
  39. */
  40. protected function _beforeSave(AbstractModel $object)
  41. {
  42. $validateRules = $object->getData('validate_rules');
  43. if (is_array($validateRules)) {
  44. $object->setData('validate_rules', $this->getSerializer()->serialize($validateRules));
  45. }
  46. return parent::_beforeSave($object);
  47. }
  48. /**
  49. * Retrieve select object for load object data
  50. *
  51. * @param string $field
  52. * @param mixed $value
  53. * @param AbstractModel $object
  54. * @return Select
  55. */
  56. protected function _getLoadSelect($field, $value, $object)
  57. {
  58. $select = parent::_getLoadSelect($field, $value, $object);
  59. $websiteId = (int)$object->getWebsite()->getId();
  60. if ($websiteId) {
  61. $connection = $this->getConnection();
  62. $columns = [];
  63. $scopeTable = $this->_getEavWebsiteTable();
  64. $describe = $connection->describeTable($scopeTable);
  65. unset($describe['attribute_id']);
  66. foreach (array_keys($describe) as $columnName) {
  67. $columns['scope_' . $columnName] = $columnName;
  68. }
  69. $conditionSql = $connection->quoteInto(
  70. $this->getMainTable() . '.attribute_id = scope_table.attribute_id AND scope_table.website_id =?',
  71. $websiteId
  72. );
  73. $select->joinLeft(['scope_table' => $scopeTable], $conditionSql, $columns);
  74. }
  75. return $select;
  76. }
  77. /**
  78. * Save attribute/form relations after attribute save
  79. *
  80. * @param \Magento\Framework\Model\AbstractModel $object
  81. * @return $this
  82. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  83. */
  84. protected function _afterSave(AbstractModel $object)
  85. {
  86. $forms = $object->getData('used_in_forms');
  87. $connection = $this->getConnection();
  88. if (is_array($forms)) {
  89. $where = ['attribute_id=?' => $object->getId()];
  90. $connection->delete($this->_getFormAttributeTable(), $where);
  91. $data = [];
  92. foreach ($forms as $formCode) {
  93. $data[] = ['form_code' => $formCode, 'attribute_id' => (int)$object->getId()];
  94. }
  95. if ($data) {
  96. $connection->insertMultiple($this->_getFormAttributeTable(), $data);
  97. }
  98. }
  99. // update sort order
  100. if (!$object->isObjectNew() && $object->dataHasChangedFor('sort_order')) {
  101. $data = ['sort_order' => $object->getSortOrder()];
  102. $where = ['attribute_id=?' => (int)$object->getId()];
  103. $connection->update($this->getTable('eav_entity_attribute'), $data, $where);
  104. }
  105. // save scope attributes
  106. $websiteId = (int)$object->getWebsite()->getId();
  107. if ($websiteId) {
  108. $table = $this->_getEavWebsiteTable();
  109. $describe = $this->getConnection()->describeTable($table);
  110. $data = [];
  111. if (!$object->getScopeWebsiteId() || $object->getScopeWebsiteId() != $websiteId) {
  112. $data = $this->getScopeValues($object);
  113. }
  114. $data['attribute_id'] = (int)$object->getId();
  115. $data['website_id'] = (int)$websiteId;
  116. unset($describe['attribute_id']);
  117. unset($describe['website_id']);
  118. $updateColumns = [];
  119. foreach (array_keys($describe) as $columnName) {
  120. $data[$columnName] = $object->getData('scope_' . $columnName);
  121. $updateColumns[] = $columnName;
  122. }
  123. $connection->insertOnDuplicate($table, $data, $updateColumns);
  124. }
  125. return parent::_afterSave($object);
  126. }
  127. /**
  128. * Return scope values for attribute and website
  129. *
  130. * @param \Magento\Eav\Model\Attribute $object
  131. * @return array
  132. */
  133. public function getScopeValues(\Magento\Eav\Model\Attribute $object)
  134. {
  135. $connection = $this->getConnection();
  136. $bind = ['attribute_id' => (int)$object->getId(), 'website_id' => (int)$object->getWebsite()->getId()];
  137. $select = $connection->select()->from(
  138. $this->_getEavWebsiteTable()
  139. )->where(
  140. 'attribute_id = :attribute_id'
  141. )->where(
  142. 'website_id = :website_id'
  143. )->limit(
  144. 1
  145. );
  146. $result = $connection->fetchRow($select, $bind);
  147. if (!$result) {
  148. $result = [];
  149. }
  150. return $result;
  151. }
  152. /**
  153. * Return forms in which the attribute
  154. *
  155. * @param \Magento\Framework\Model\AbstractModel $object
  156. * @return array
  157. */
  158. public function getUsedInForms(AbstractModel $object)
  159. {
  160. $connection = $this->getConnection();
  161. $bind = ['attribute_id' => (int)$object->getId()];
  162. $select = $connection->select()->from(
  163. $this->_getFormAttributeTable(),
  164. 'form_code'
  165. )->where(
  166. 'attribute_id = :attribute_id'
  167. );
  168. return $connection->fetchCol($select, $bind);
  169. }
  170. }