11 use Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory;
39 private $attrGroupCollectionFactory;
46 private $attributeMapper;
60 private $_generalGroupName =
'General';
67 private $defaultGroupIdAssociations = [
'general' => 1];
74 private $_defaultGroupName =
'Default';
81 private $_defaultAttributeSetName =
'Default';
95 CollectionFactory $attrGroupCollectionFactory
98 $this->attrGroupCollectionFactory = $attrGroupCollectionFactory;
120 return $this->attrGroupCollectionFactory->create();
130 $this->cache->clean([\
Magento\Eav\Model\Cache\Type::CACHE_TAG]);
142 foreach ($this->defaultGroupIdAssociations as $defaultGroupCode =>
$defaultGroupId) {
143 foreach ($setIds as $set) {
144 $groupId = $this->setup->getTableRow(
145 'eav_attribute_group',
146 'attribute_group_code',
148 'attribute_group_id',
153 $groupId = $this->setup->getTableRow(
154 'eav_attribute_group',
160 $this->setup->updateTableRow(
161 'eav_attribute_group',
162 'attribute_group_id',
187 'entity_type_code' =>
$code,
188 'entity_model' =>
$params[
'entity_model'],
189 'attribute_model' => $this->_getValue(
$params,
'attribute_model'),
190 'entity_table' => $this->_getValue(
$params,
'table',
'eav_entity'),
191 'value_table_prefix' => $this->_getValue(
$params,
'table_prefix'),
192 'entity_id_field' => $this->_getValue(
$params,
'id_field'),
193 'increment_model' => $this->_getValue(
$params,
'increment_model'),
194 'increment_per_store' => $this->_getValue(
$params,
'increment_per_store', 0),
195 'increment_pad_length' => $this->_getValue(
$params,
'increment_pad_length', 8),
196 'increment_pad_char' => $this->_getValue(
$params,
'increment_pad_char', 0),
197 'additional_attribute_table' => $this->_getValue(
$params,
'additional_attribute_table'),
198 'entity_attribute_collection' => $this->_getValue(
$params,
'entity_attribute_collection'),
200 if (isset(
$params[
'entity_type_id'])) {
207 $this->setup->getConnection()->insert(
208 $this->setup->getTable(
'eav_entity_type'),
213 if (isset(
$params[
'entity_type_id'])) {
233 $this->setup->updateTableRow(
252 return $this->setup->getTableRow(
254 is_numeric(
$id) ?
'entity_type_id' :
'entity_type_code',
287 if (is_numeric(
$id)) {
288 $this->setup->deleteTableRow(
'eav_entity_type',
'entity_type_id',
$id);
290 $this->setup->deleteTableRow(
'eav_entity_type',
'entity_type_code', (
string)
$id);
307 if (!is_numeric($sortOrder)) {
309 $select = $this->setup->getConnection()->select()->from(
310 $this->setup->getTable(
'eav_attribute_set'),
313 'entity_type_id = :entity_type_id' 316 $sortOrder = $this->setup->getConnection()->fetchOne(
$select, $bind) + 1;
335 'attribute_set_name' =>
$name,
339 if ($setId !==
null) {
340 $data[
'attribute_set_id'] = $setId;
347 $this->setup->getConnection()->insert(
348 $this->setup->getTable(
'eav_attribute_set'),
369 $this->setup->updateTableRow(
391 return $this->setup->getTableRow(
393 is_numeric(
$id) ?
'attribute_set_id' :
'attribute_set_name',
411 if (!is_numeric($setId)) {
414 if (!is_numeric($setId)) {
415 throw new LocalizedException(
__(
'The attribute set ID is incorrect. Verify the ID and try again.'));
430 $this->setup->deleteTableRow(
461 $select = $this->setup->getConnection()->select()
462 ->from($this->setup->getTable(
'eav_attribute_set'),
'attribute_set_id');
467 $select->where(
'entity_type_id = :entity_type_id');
470 return $this->setup->getConnection()->fetchCol(
$select, $bind);
483 $where =
'entity_type_id = :entity_type';
485 $where =
'entity_type_code = :entity_type';
487 $select = $this->setup->getConnection()->select()->from(
488 $this->setup->getTable(
'eav_entity_type'),
489 'default_attribute_set_id' 494 return $this->setup->getConnection()->fetchOne(
$select, $bind);
509 if (!is_numeric($sortOrder)) {
511 $select = $this->setup->getConnection()->select()->from(
512 $this->setup->getTable(
'eav_attribute_group'),
515 'attribute_set_id = :attribute_set_id' 518 $sortOrder = $this->setup->getConnection()->fetchOne(
$select, $bind) + 1;
536 $data = [
'attribute_set_id' => $setId,
'attribute_group_name' =>
$name];
539 if (isset($this->defaultGroupIdAssociations[$attributeGroupCode])) {
540 $data[
'default_id'] = $this->defaultGroupIdAssociations[$attributeGroupCode];
543 if ($sortOrder !==
null) {
544 $data[
'sort_order'] = $sortOrder;
551 if ($sortOrder ===
null) {
554 if (empty(
$data[
'attribute_group_code'])) {
555 if (empty($attributeGroupCode)) {
557 $attributeGroupCode = md5(
$name);
559 $data[
'attribute_group_code'] = $attributeGroupCode;
561 $this->setup->getConnection()->insert(
562 $this->setup->getTable(
'eav_attribute_group'),
577 return trim(preg_replace(
'/[^a-z0-9]+/',
'-', strtolower($groupName)),
'-');
592 $this->setup->updateTableRow(
593 'eav_attribute_group',
594 'attribute_group_id',
616 if (is_numeric(
$id)) {
617 $searchField =
'attribute_group_id';
620 if (isset($this->defaultGroupIdAssociations[
$id])) {
621 $searchField =
'default_id';
622 $id = $this->defaultGroupIdAssociations[
$id];
624 $searchField =
'attribute_group_code';
628 return $this->setup->getTableRow(
629 'eav_attribute_group',
650 return $this->setup->getTableRow(
651 'eav_attribute_group',
652 'attribute_group_code',
680 throw new LocalizedException(
__(
'The attribute group ID is incorrect. Verify the ID and try again.'));
695 $this->setup->deleteTableRow(
696 'eav_attribute_group',
697 'attribute_group_id',
717 $select = $this->setup->getConnection()->select()->from(
718 $this->setup->getTable(
'eav_attribute_group'),
721 'attribute_set_id = :attribute_set_id' 723 [
'default_id ' . \
Magento\Framework\DB\Select::SQL_DESC,
'sort_order']
728 return $this->setup->getConnection()->fetchOne(
$select, $bind);
742 $select = $this->setup->getConnection()->select()->from(
743 $this->setup->getTable(
'eav_entity_attribute'),
744 [
'count' =>
'COUNT(*)']
746 'attribute_group_id = ?',
749 'entity_type_id = ?',
752 'attribute_set_id = ?',
756 return $this->setup->getConnection()->fetchOne(
$select);
769 private function _getValue($array, $key, $default =
null)
771 if (isset($array[$key]) && is_bool($array[$key])) {
772 $array[$key] = (int)$array[$key];
774 return isset($array[$key]) ? $array[$key] : $default;
784 private function _validateAttributeData(
$data)
786 $minLength = \Magento\Eav\Model\Entity\Attribute::ATTRIBUTE_CODE_MIN_LENGTH;
787 $maxLength = \Magento\Eav\Model\Entity\Attribute::ATTRIBUTE_CODE_MAX_LENGTH;
793 [
'min' => $minLength,
'max' => $maxLength]
796 if (!$isAllowedLength) {
798 'An attribute code must not be less than %1 and more than %2 characters.',
803 throw new LocalizedException($errorMessage);
823 $data = array_replace(
828 $this->_validateAttributeData(
$data);
830 $sortOrder = isset(
$attr[
'sort_order']) ?
$attr[
'sort_order'] :
null;
835 $this->_insertAttribute(
$data);
838 if (!empty(
$attr[
'group']) || empty(
$attr[
'user_defined'])) {
839 $select = $this->setup->getConnection()->select()->from(
840 $this->setup->getTable(
'eav_attribute_set')
842 'entity_type_id = :entity_type_id' 845 foreach ($sets as $set) {
846 if (!empty(
$attr[
'group'])) {
850 $set[
'attribute_set_id'],
858 $set[
'attribute_set_id'],
859 $this->_generalGroupName,
867 if (isset(
$attr[
'option']) && is_array(
$attr[
'option'])) {
886 $optionTable = $this->setup->getTable(
'eav_attribute_option');
887 $optionValueTable = $this->setup->getTable(
'eav_attribute_option_value');
894 $condition = [
'option_id =?' => $intOptionId];
895 $this->setup->getConnection()->delete($optionTable, $condition);
902 'attribute_id' =>
$option[
'attribute_id'],
905 $this->setup->getConnection()->insert($optionTable,
$data);
906 $intOptionId = $this->setup->getConnection()->lastInsertId($optionTable);
911 $this->setup->getConnection()->update(
914 [
'option_id=?' => $intOptionId]
920 throw new \Magento\Framework\Exception\LocalizedException(
921 __(
"The default option isn't defined. Set the option and try again.")
924 $condition = [
'option_id =?' => $intOptionId];
925 $this->setup->getConnection()->delete($optionValueTable, $condition);
928 $this->setup->getConnection()->insert($optionValueTable,
$data);
934 $data = [
'attribute_id' =>
$option[
'attribute_id'],
'sort_order' => $sortOrder];
935 $this->setup->getConnection()->insert($optionTable,
$data);
936 $intOptionId = $this->setup->getConnection()->lastInsertId($optionTable);
938 $data = [
'option_id' => $intOptionId,
'store_id' => 0,
'value' =>
$label];
939 $this->setup->getConnection()->insert($optionValueTable,
$data);
974 if ($sortOrder !==
null) {
975 $this->setup->updateTableRow(
976 'eav_entity_attribute',
984 $attributeFields = $this->_getAttributeTableFields();
985 if (is_array($field)) {
987 foreach ($field as $k => $v) {
988 if (isset($attributeFields[$k])) {
989 $bind[$k] = $this->setup->getConnection()->prepareColumnValue(
990 $attributeFields[$k],
1000 if (!isset($attributeFields[$field])) {
1005 if (
false === $attributeId) {
1006 throw new LocalizedException(
__(
'Attribute with ID: "%1" does not exist',
$id));
1009 $this->setup->updateTableRow(
1035 if (!$additionalTable) {
1038 $additionalTableExists = $this->setup->getConnection()->isTableExists(
1039 $this->setup->getTable($additionalTable)
1041 if (!$additionalTableExists) {
1044 $attributeFields = $this->setup->getConnection()->describeTable(
1045 $this->setup->getTable($additionalTable)
1047 if (is_array($field)) {
1049 foreach ($field as $k => $v) {
1050 if (isset($attributeFields[$k])) {
1051 $bind[$k] = $this->setup->getConnection()->prepareColumnValue(
1052 $attributeFields[$k],
1062 if (!isset($attributeFields[$field])) {
1068 if (
false === $attributeId) {
1069 throw new LocalizedException(
__(
'Attribute with ID: "%1" does not exist',
$id));
1071 $this->setup->updateTableRow(
1072 $this->setup->getTable($additionalTable),
1096 $setupCache = $this->setup->getSetupCache();
1097 $mainTable = $this->setup->getTable(
'eav_attribute');
1098 if (is_array($field)) {
1099 $oldRow = $setupCache->has($mainTable,
$attribute[
'entity_type_id'],
$attribute[
'attribute_code']) ?
1102 $newRowData = array_merge($oldRow, $field);
1103 $setupCache->setRow(
1110 $setupCache->setField(
1132 $idField = is_numeric(
$id) ?
'attribute_id' :
'attribute_code';
1133 if (!$additionalTable) {
1134 return $this->setup->getTableRow(
'eav_attribute', $idField,
$id, $field,
'entity_type_id',
$entityTypeId);
1137 $mainTable = $this->setup->getTable(
'eav_attribute');
1138 $setupCache = $this->setup->getSetupCache();
1140 $additionalTable = $this->setup->getTable($additionalTable);
1142 $select = $this->setup->getConnection()->select()->from(
1143 [
'main' => $mainTable]
1145 [
'additional' => $additionalTable],
1146 'main.attribute_id = additional.attribute_id' 1148 "main.{$idField} = :id" 1150 'main.entity_type_id = :entity_type_id' 1153 $row = $this->setup->getConnection()->fetchRow(
$select, $bind);
1163 if ($field !==
null) {
1164 return isset(
$row[$field]) ?
$row[$field] :
false;
1179 if (!is_numeric(
$id)) {
1182 if (!is_numeric(
$id)) {
1197 $entityKeyName = is_numeric(
$entityTypeId) ?
'entity_type_id' :
'entity_type_code';
1198 $attributeKeyName = is_numeric(
$id) ?
'attribute_id' :
'attribute_code';
1201 $select = $this->setup->getConnection()->select()->from(
1202 [
'entity_type' => $this->setup->getTable(
'eav_entity_type')],
1205 [
'attribute' => $this->setup->getTable(
'eav_attribute')],
1206 'attribute.entity_type_id = entity_type.entity_type_id',
1209 "entity_type.{$entityKeyName} = :entity_type_id" 1211 "attribute.{$attributeKeyName} = :id" 1216 $result = $this->setup->getConnection()->fetchRow(
$select, $bind);
1219 if (
$result[
'backend_type'] !=
'static') {
1237 $mainTable = $this->setup->getTable(
'eav_attribute');
1240 $this->setup->deleteTableRow(
'eav_attribute',
'attribute_id',
$attribute[
'attribute_id']);
1241 $setupCache = $this->setup->getSetupCache();
1242 if ($setupCache->has($mainTable,
$attribute[
'entity_type_id'],
$attribute[
'attribute_code'])) {
1260 if (!is_numeric($sortOrder)) {
1262 $select = $this->setup->getConnection()->select()->from(
1263 $this->setup->getTable(
'eav_entity_attribute'),
1266 'attribute_group_id = :attribute_group_id' 1269 $sortOrder = $this->setup->getConnection()->fetchOne(
$select, $bind) + 1;
1291 $table = $this->setup->getTable(
'eav_entity_attribute');
1293 $bind = [
'attribute_set_id' => $setId,
'attribute_id' => $attributeId];
1294 $select = $this->setup->getConnection()->select()->from(
1297 'attribute_set_id = :attribute_set_id' 1299 'attribute_id = :attribute_id' 1301 $result = $this->setup->getConnection()->fetchRow(
$select, $bind);
1305 $where = [
'entity_attribute_id =?' =>
$result[
'entity_attribute_id']];
1307 $this->setup->getConnection()->update(
$table,
$data, $where);
1312 'attribute_set_id' => $setId,
1314 'attribute_id' => $attributeId,
1318 $this->setup->getConnection()->insert(
$table,
$data);
1343 'attribute_set_id' => $setId,
1345 'attribute_id' => $attributeId,
1348 $bind = [
'entity_type_id' =>
$entityType,
'attribute_set_id' => $setId,
'attribute_id' => $attributeId];
1349 $select = $this->setup->getConnection()->select()->from(
1350 $this->setup->getTable(
'eav_entity_attribute')
1352 'entity_type_id = :entity_type_id' 1354 'attribute_set_id = :attribute_set_id' 1356 'attribute_id = :attribute_id' 1358 $row = $this->setup->getConnection()->fetchRow(
$select, $bind);
1361 if ($sortOrder !==
null) {
1362 $data[
'sort_order'] = $sortOrder;
1365 $this->setup->getConnection()->update(
1366 $this->setup->getTable(
'eav_entity_attribute'),
1368 $this->setup->getConnection()->quoteInto(
'entity_attribute_id=?',
$row[
'entity_attribute_id'])
1371 if ($sortOrder ===
null) {
1372 $select = $this->setup->getConnection()->select()->from(
1373 $this->setup->getTable(
'eav_entity_attribute'),
1376 'entity_type_id = :entity_type_id' 1378 'attribute_set_id = :attribute_set_id' 1380 'attribute_id = :attribute_id' 1383 $sortOrder = $this->setup->getConnection()->fetchOne(
$select, $bind) + 10;
1385 $sortOrder = is_numeric($sortOrder) ? $sortOrder : 1;
1386 $data[
'sort_order'] = $sortOrder;
1387 $this->setup->getConnection()->insert(
1388 $this->setup->getTable(
'eav_entity_attribute'),
1420 if ($entities ===
null) {
1424 foreach ($entities as $entityName =>
$entity) {
1427 $frontendPrefix = isset(
$entity[
'frontend_prefix']) ?
$entity[
'frontend_prefix'] :
'';
1428 $backendPrefix = isset(
$entity[
'backend_prefix']) ?
$entity[
'backend_prefix'] :
'';
1429 $sourcePrefix = isset(
$entity[
'source_prefix']) ?
$entity[
'source_prefix'] :
'';
1431 if (is_array(
$entity[
'attributes']) && !empty(
$entity[
'attributes'])) {
1432 foreach (
$entity[
'attributes'] as $attrCode =>
$attr) {
1433 if (!empty(
$attr[
'backend'])) {
1434 if (
'_' ===
$attr[
'backend']) {
1435 $attr[
'backend'] = $backendPrefix;
1437 $attr[
'backend'] = $backendPrefix .
$attr[
'backend'];
1440 if (!empty(
$attr[
'frontend'])) {
1441 if (
'_' ===
$attr[
'frontend']) {
1442 $attr[
'frontend'] = $frontendPrefix;
1444 $attr[
'frontend'] = $frontendPrefix .
$attr[
'frontend'];
1447 if (!empty(
$attr[
'source'])) {
1448 if (
'_' ===
$attr[
'source']) {
1449 $attr[
'source'] = $sourcePrefix;
1451 $attr[
'source'] = $sourcePrefix .
$attr[
'source'];
1469 private function _getAttributeTableFields()
1471 return $this->setup->getConnection()->describeTable(
1472 $this->setup->getTable(
'eav_attribute')
1482 private function _insertAttribute(array
$data)
1486 $fields = $this->_getAttributeTableFields();
1488 foreach (
$data as $k => $v) {
1490 $bind[$k] = $this->setup->getConnection()->prepareColumnValue(
$fields[$k], $v);
1497 $this->setup->getConnection()->insert(
1498 $this->setup->getTable(
'eav_attribute'),
1501 $attributeId = $this->setup->getConnection()->lastInsertId(
1502 $this->setup->getTable(
'eav_attribute')
1504 $this->_insertAttributeAdditionalData(
1505 $data[
'entity_type_id'],
1506 array_merge([
'attribute_id' => $attributeId],
$data)
1522 if (!$additionalTable) {
1525 $additionalTableExists = $this->setup->getConnection()->isTableExists(
1526 $this->setup->getTable($additionalTable)
1528 if ($additionalTable && $additionalTableExists) {
1530 $fields = $this->setup->getConnection()->describeTable(
1531 $this->setup->getTable($additionalTable)
1533 foreach (
$data as $k => $v) {
1535 $bind[$k] = $this->setup->getConnection()->prepareColumnValue(
$fields[$k], $v);
1541 $this->setup->getConnection()->insert(
1542 $this->setup->getTable($additionalTable),
getAttributeSet($entityTypeId, $id, $field=null)
getAttributeGroupId($entityTypeId, $setId, $groupId)
getEntityTypeId($entityTypeId)
convertToAttributeGroupCode($groupName)
addAttributeToGroup($entityType, $setId, $groupId, $attributeId, $sortOrder=null)
removeAttribute($entityTypeId, $code)
getAttributeSortOrder($entityTypeId, $setId, $groupId, $sortOrder=null)
getAttributeSetId($entityTypeId, $setId)
getDefaultAttributeSetId($entityType)
getAttributeId($entityTypeId, $id)
elseif(isset( $params[ 'redirect_parent']))
updateAttributeGroup($entityTypeId, $setId, $id, $field, $value=null)
addAttributeGroup($entityTypeId, $setId, $name, $sortOrder=null)
getAttribute($entityTypeId, $id, $field=null)
getAttributeTable($entityTypeId, $id)
__construct(ModuleDataSetupInterface $setup, Context $context, CacheInterface $cache, CollectionFactory $attrGroupCollectionFactory)
setDefaultSetToEntityType($entityType, $attributeSet='Default')
getAttributeGroupCollectionFactory()
installEntities($entities=null)
addAttribute($entityTypeId, $code, array $attr)
addAttributeOption($option)
getAttributeGroupSortOrder($entityTypeId, $setId, $sortOrder=null)
getEntityType($id, $field=null)
getAllAttributeSetIds($entityTypeId=null)
addAttributeToSet($entityTypeId, $setId, $groupId, $attributeId, $sortOrder=null)
getAttributesNumberInGroup($entityTypeId, $setId, $groupId)
updateEntityType($code, $field, $value=null)
getDefaultAttributeGroupId($entityType, $attributeSetId=null)
addAttributeSet($entityTypeId, $name, $sortOrder=null, $setId=null)
getAttributeSetSortOrder($entityTypeId, $sortOrder=null)
removeAttributeGroup($entityTypeId, $setId, $id)
getAttributeGroupByCode($entityTypeId, $setId, $code, $field=null)
removeAttributeSet($entityTypeId, $id)
static is($value, $classBaseName, array $args=array(), $namespaces=array())
addEntityType($code, array $params)
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
getAttributeGroup($entityTypeId, $setId, $id, $field=null)
updateAttribute($entityTypeId, $id, $field, $value=null, $sortOrder=null)
updateAttributeSet($entityTypeId, $id, $field, $value=null)
if(!isset($_GET['name'])) $name