Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions
EavSetup Class Reference
Inheritance diagram for EavSetup:
CategorySetup CustomerSetup QuoteSetup SalesSetup

Public Member Functions

 __construct (ModuleDataSetupInterface $setup, Context $context, CacheInterface $cache, CollectionFactory $attrGroupCollectionFactory)
 
 getSetup ()
 
 getAttributeGroupCollectionFactory ()
 
 cleanCache ()
 
 installDefaultGroupIds ()
 
 addEntityType ($code, array $params)
 
 updateEntityType ($code, $field, $value=null)
 
 getEntityType ($id, $field=null)
 
 getEntityTypeId ($entityTypeId)
 
 removeEntityType ($id)
 
 getAttributeSetSortOrder ($entityTypeId, $sortOrder=null)
 
 addAttributeSet ($entityTypeId, $name, $sortOrder=null, $setId=null)
 
 updateAttributeSet ($entityTypeId, $id, $field, $value=null)
 
 getAttributeSet ($entityTypeId, $id, $field=null)
 
 getAttributeSetId ($entityTypeId, $setId)
 
 removeAttributeSet ($entityTypeId, $id)
 
 setDefaultSetToEntityType ($entityType, $attributeSet='Default')
 
 getAllAttributeSetIds ($entityTypeId=null)
 
 getDefaultAttributeSetId ($entityType)
 
 getAttributeGroupSortOrder ($entityTypeId, $setId, $sortOrder=null)
 
 addAttributeGroup ($entityTypeId, $setId, $name, $sortOrder=null)
 
 convertToAttributeGroupCode ($groupName)
 
 updateAttributeGroup ($entityTypeId, $setId, $id, $field, $value=null)
 
 getAttributeGroup ($entityTypeId, $setId, $id, $field=null)
 
 getAttributeGroupByCode ($entityTypeId, $setId, $code, $field=null)
 
 getAttributeGroupId ($entityTypeId, $setId, $groupId)
 
 removeAttributeGroup ($entityTypeId, $setId, $id)
 
 getDefaultAttributeGroupId ($entityType, $attributeSetId=null)
 
 getAttributesNumberInGroup ($entityTypeId, $setId, $groupId)
 
 addAttribute ($entityTypeId, $code, array $attr)
 
 addAttributeOption ($option)
 
 updateAttribute ($entityTypeId, $id, $field, $value=null, $sortOrder=null)
 
 getAttribute ($entityTypeId, $id, $field=null)
 
 getAttributeId ($entityTypeId, $id)
 
 getAttributeTable ($entityTypeId, $id)
 
 removeAttribute ($entityTypeId, $code)
 
 getAttributeSortOrder ($entityTypeId, $setId, $groupId, $sortOrder=null)
 
 addAttributeToSet ($entityTypeId, $setId, $groupId, $attributeId, $sortOrder=null)
 
 addAttributeToGroup ($entityType, $setId, $groupId, $attributeId, $sortOrder=null)
 
 getDefaultEntities ()
 
 installEntities ($entities=null)
 

Detailed Description

@api @SuppressWarnings(PHPMD.ExcessiveClassComplexity) @SuppressWarnings(PHPMD.CouplingBetweenObjects) @codeCoverageIgnore

Since
100.0.2

Definition at line 25 of file EavSetup.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( ModuleDataSetupInterface  $setup,
Context  $context,
CacheInterface  $cache,
CollectionFactory  $attrGroupCollectionFactory 
)

Init

Parameters
ModuleDataSetupInterface$setup
Context$context
CacheInterface$cache
CollectionFactory$attrGroupCollectionFactory

Definition at line 91 of file EavSetup.php.

96  {
97  $this->cache = $cache;
98  $this->attrGroupCollectionFactory = $attrGroupCollectionFactory;
99  $this->attributeMapper = $context->getAttributeMapper();
100  $this->setup = $setup;
101  }

Member Function Documentation

◆ addAttribute()

addAttribute (   $entityTypeId,
  $code,
array  $attr 
)

Add attribute to an entity type

If attribute is system will add to all existing attribute sets

Parameters
string | integer$entityTypeId
string$code
array$attr
Returns
$this

Definition at line 819 of file EavSetup.php.

820  {
822 
823  $data = array_replace(
824  ['entity_type_id' => $entityTypeId, 'attribute_code' => $code],
825  $this->attributeMapper->map($attr, $entityTypeId)
826  );
827 
828  $this->_validateAttributeData($data);
829 
830  $sortOrder = isset($attr['sort_order']) ? $attr['sort_order'] : null;
831  $attributeId = $this->getAttribute($entityTypeId, $code, 'attribute_id');
832  if ($attributeId) {
833  $this->updateAttribute($entityTypeId, $attributeId, $data, null, $sortOrder);
834  } else {
835  $this->_insertAttribute($data);
836  }
837 
838  if (!empty($attr['group']) || empty($attr['user_defined'])) {
839  $select = $this->setup->getConnection()->select()->from(
840  $this->setup->getTable('eav_attribute_set')
841  )->where(
842  'entity_type_id = :entity_type_id'
843  );
844  $sets = $this->setup->getConnection()->fetchAll($select, ['entity_type_id' => $entityTypeId]);
845  foreach ($sets as $set) {
846  if (!empty($attr['group'])) {
847  $this->addAttributeGroup($entityTypeId, $set['attribute_set_id'], $attr['group']);
848  $this->addAttributeToSet(
850  $set['attribute_set_id'],
851  $attr['group'],
852  $code,
853  $sortOrder
854  );
855  } else {
856  $this->addAttributeToSet(
858  $set['attribute_set_id'],
859  $this->_generalGroupName,
860  $code,
861  $sortOrder
862  );
863  }
864  }
865  }
866 
867  if (isset($attr['option']) && is_array($attr['option'])) {
868  $option = $attr['option'];
869  $option['attribute_id'] = $this->getAttributeId($entityTypeId, $code);
870  $this->addAttributeOption($option);
871  }
872 
873  return $this;
874  }
getEntityTypeId($entityTypeId)
Definition: EavSetup.php:267
getAttributeId($entityTypeId, $id)
Definition: EavSetup.php:1177
$attr
Definition: text.phtml:8
addAttributeGroup($entityTypeId, $setId, $name, $sortOrder=null)
Definition: EavSetup.php:533
getAttribute($entityTypeId, $id, $field=null)
Definition: EavSetup.php:1128
addAttributeToSet($entityTypeId, $setId, $groupId, $attributeId, $sortOrder=null)
Definition: EavSetup.php:1285
updateAttribute($entityTypeId, $id, $field, $value=null, $sortOrder=null)
Definition: EavSetup.php:954
$code
Definition: info.phtml:12

◆ addAttributeGroup()

addAttributeGroup (   $entityTypeId,
  $setId,
  $name,
  $sortOrder = null 
)

Add Attribute Group

Parameters
int | string$entityTypeId
int | string$setId
string$name
int$sortOrder
Returns
$this

Definition at line 533 of file EavSetup.php.

534  {
535  $setId = $this->getAttributeSetId($entityTypeId, $setId);
536  $data = ['attribute_set_id' => $setId, 'attribute_group_name' => $name];
537  $attributeGroupCode = $this->convertToAttributeGroupCode($name);
538 
539  if (isset($this->defaultGroupIdAssociations[$attributeGroupCode])) {
540  $data['default_id'] = $this->defaultGroupIdAssociations[$attributeGroupCode];
541  }
542 
543  if ($sortOrder !== null) {
544  $data['sort_order'] = $sortOrder;
545  }
546 
547  $groupId = $this->getAttributeGroup($entityTypeId, $setId, $attributeGroupCode, 'attribute_group_id');
548  if ($groupId) {
550  } else {
551  if ($sortOrder === null) {
552  $data['sort_order'] = $this->getAttributeGroupSortOrder($entityTypeId, $setId, $sortOrder);
553  }
554  if (empty($data['attribute_group_code'])) {
555  if (empty($attributeGroupCode)) {
556  // in the following code md5 is not used for security purposes
557  $attributeGroupCode = md5($name);
558  }
559  $data['attribute_group_code'] = $attributeGroupCode;
560  }
561  $this->setup->getConnection()->insert(
562  $this->setup->getTable('eav_attribute_group'),
563  $data
564  );
565  }
566 
567  return $this;
568  }
convertToAttributeGroupCode($groupName)
Definition: EavSetup.php:575
getAttributeSetId($entityTypeId, $setId)
Definition: EavSetup.php:409
updateAttributeGroup($entityTypeId, $setId, $id, $field, $value=null)
Definition: EavSetup.php:590
getAttributeGroupSortOrder($entityTypeId, $setId, $sortOrder=null)
Definition: EavSetup.php:507
getAttributeGroup($entityTypeId, $setId, $id, $field=null)
Definition: EavSetup.php:614
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ addAttributeOption()

addAttributeOption (   $option)

Add Attribute Option

Parameters
array$option
Returns
void
Exceptions

Definition at line 884 of file EavSetup.php.

885  {
886  $optionTable = $this->setup->getTable('eav_attribute_option');
887  $optionValueTable = $this->setup->getTable('eav_attribute_option_value');
888 
889  if (isset($option['value'])) {
890  foreach ($option['value'] as $optionId => $values) {
891  $intOptionId = (int)$optionId;
892  if (!empty($option['delete'][$optionId])) {
893  if ($intOptionId) {
894  $condition = ['option_id =?' => $intOptionId];
895  $this->setup->getConnection()->delete($optionTable, $condition);
896  }
897  continue;
898  }
899 
900  if (!$intOptionId) {
901  $data = [
902  'attribute_id' => $option['attribute_id'],
903  'sort_order' => isset($option['order'][$optionId]) ? $option['order'][$optionId] : 0,
904  ];
905  $this->setup->getConnection()->insert($optionTable, $data);
906  $intOptionId = $this->setup->getConnection()->lastInsertId($optionTable);
907  } else {
908  $data = [
909  'sort_order' => isset($option['order'][$optionId]) ? $option['order'][$optionId] : 0,
910  ];
911  $this->setup->getConnection()->update(
912  $optionTable,
913  $data,
914  ['option_id=?' => $intOptionId]
915  );
916  }
917 
918  // Default value
919  if (!isset($values[0])) {
920  throw new \Magento\Framework\Exception\LocalizedException(
921  __("The default option isn't defined. Set the option and try again.")
922  );
923  }
924  $condition = ['option_id =?' => $intOptionId];
925  $this->setup->getConnection()->delete($optionValueTable, $condition);
926  foreach ($values as $storeId => $value) {
927  $data = ['option_id' => $intOptionId, 'store_id' => $storeId, 'value' => $value];
928  $this->setup->getConnection()->insert($optionValueTable, $data);
929  }
930  }
931  } elseif (isset($option['values'])) {
932  foreach ($option['values'] as $sortOrder => $label) {
933  // add option
934  $data = ['attribute_id' => $option['attribute_id'], 'sort_order' => $sortOrder];
935  $this->setup->getConnection()->insert($optionTable, $data);
936  $intOptionId = $this->setup->getConnection()->lastInsertId($optionTable);
937 
938  $data = ['option_id' => $intOptionId, 'store_id' => 0, 'value' => $label];
939  $this->setup->getConnection()->insert($optionValueTable, $data);
940  }
941  }
942  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$values
Definition: options.phtml:88
__()
Definition: __.php:13
$label
Definition: details.phtml:21
$value
Definition: gender.phtml:16

◆ addAttributeSet()

addAttributeSet (   $entityTypeId,
  $name,
  $sortOrder = null,
  $setId = null 
)

Add Attribute Set

Parameters
int | string$entityTypeId
string$name
int$sortOrder
int$setId
Returns
$this

Definition at line 331 of file EavSetup.php.

332  {
333  $data = [
334  'entity_type_id' => $this->getEntityTypeId($entityTypeId),
335  'attribute_set_name' => $name,
336  'sort_order' => $this->getAttributeSetSortOrder($entityTypeId, $sortOrder),
337  ];
338 
339  if ($setId !== null) {
340  $data['attribute_set_id'] = $setId;
341  }
342 
343  $setId = $this->getAttributeSet($entityTypeId, $name, 'attribute_set_id');
344  if ($setId) {
345  $this->updateAttributeSet($entityTypeId, $setId, $data);
346  } else {
347  $this->setup->getConnection()->insert(
348  $this->setup->getTable('eav_attribute_set'),
349  $data
350  );
351 
352  $this->addAttributeGroup($entityTypeId, $name, $this->_generalGroupName);
353  }
354 
355  return $this;
356  }
getAttributeSet($entityTypeId, $id, $field=null)
Definition: EavSetup.php:389
getEntityTypeId($entityTypeId)
Definition: EavSetup.php:267
addAttributeGroup($entityTypeId, $setId, $name, $sortOrder=null)
Definition: EavSetup.php:533
getAttributeSetSortOrder($entityTypeId, $sortOrder=null)
Definition: EavSetup.php:305
updateAttributeSet($entityTypeId, $id, $field, $value=null)
Definition: EavSetup.php:367
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ addAttributeToGroup()

addAttributeToGroup (   $entityType,
  $setId,
  $groupId,
  $attributeId,
  $sortOrder = null 
)

Add or update attribute to group

Parameters
int | string$entityType
int | string$setId
int | string$groupId
int | string$attributeId
int$sortOrder
Returns
$this

Definition at line 1334 of file EavSetup.php.

1335  {
1337  $setId = $this->getAttributeSetId($entityType, $setId);
1338  $groupId = $this->getAttributeGroupId($entityType, $setId, $groupId);
1339  $attributeId = $this->getAttributeId($entityType, $attributeId);
1340 
1341  $data = [
1342  'entity_type_id' => $entityType,
1343  'attribute_set_id' => $setId,
1344  'attribute_group_id' => $groupId,
1345  'attribute_id' => $attributeId,
1346  ];
1347 
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')
1351  )->where(
1352  'entity_type_id = :entity_type_id'
1353  )->where(
1354  'attribute_set_id = :attribute_set_id'
1355  )->where(
1356  'attribute_id = :attribute_id'
1357  );
1358  $row = $this->setup->getConnection()->fetchRow($select, $bind);
1359  if ($row) {
1360  // update
1361  if ($sortOrder !== null) {
1362  $data['sort_order'] = $sortOrder;
1363  }
1364 
1365  $this->setup->getConnection()->update(
1366  $this->setup->getTable('eav_entity_attribute'),
1367  $data,
1368  $this->setup->getConnection()->quoteInto('entity_attribute_id=?', $row['entity_attribute_id'])
1369  );
1370  } else {
1371  if ($sortOrder === null) {
1372  $select = $this->setup->getConnection()->select()->from(
1373  $this->setup->getTable('eav_entity_attribute'),
1374  'MAX(sort_order)'
1375  )->where(
1376  'entity_type_id = :entity_type_id'
1377  )->where(
1378  'attribute_set_id = :attribute_set_id'
1379  )->where(
1380  'attribute_id = :attribute_id'
1381  );
1382 
1383  $sortOrder = $this->setup->getConnection()->fetchOne($select, $bind) + 10;
1384  }
1385  $sortOrder = is_numeric($sortOrder) ? $sortOrder : 1;
1386  $data['sort_order'] = $sortOrder;
1387  $this->setup->getConnection()->insert(
1388  $this->setup->getTable('eav_entity_attribute'),
1389  $data
1390  );
1391  }
1392 
1393  return $this;
1394  }
getAttributeGroupId($entityTypeId, $setId, $groupId)
Definition: EavSetup.php:669
getEntityTypeId($entityTypeId)
Definition: EavSetup.php:267
getAttributeSetId($entityTypeId, $setId)
Definition: EavSetup.php:409
getAttributeId($entityTypeId, $id)
Definition: EavSetup.php:1177

◆ addAttributeToSet()

addAttributeToSet (   $entityTypeId,
  $setId,
  $groupId,
  $attributeId,
  $sortOrder = null 
)

Add Attribute to All Groups on Attribute Set

Parameters
int | string$entityTypeId
int | string$setId
int | string$groupId
int | string$attributeId
int$sortOrder
Returns
$this

Definition at line 1285 of file EavSetup.php.

1286  {
1288  $setId = $this->getAttributeSetId($entityTypeId, $setId);
1290  $attributeId = $this->getAttributeId($entityTypeId, $attributeId);
1291  $table = $this->setup->getTable('eav_entity_attribute');
1292 
1293  $bind = ['attribute_set_id' => $setId, 'attribute_id' => $attributeId];
1294  $select = $this->setup->getConnection()->select()->from(
1295  $table
1296  )->where(
1297  'attribute_set_id = :attribute_set_id'
1298  )->where(
1299  'attribute_id = :attribute_id'
1300  );
1301  $result = $this->setup->getConnection()->fetchRow($select, $bind);
1302 
1303  if ($result) {
1304  if ($result['attribute_group_id'] != $groupId) {
1305  $where = ['entity_attribute_id =?' => $result['entity_attribute_id']];
1306  $data = ['attribute_group_id' => $groupId];
1307  $this->setup->getConnection()->update($table, $data, $where);
1308  }
1309  } else {
1310  $data = [
1311  'entity_type_id' => $entityTypeId,
1312  'attribute_set_id' => $setId,
1313  'attribute_group_id' => $groupId,
1314  'attribute_id' => $attributeId,
1315  'sort_order' => $this->getAttributeSortOrder($entityTypeId, $setId, $groupId, $sortOrder),
1316  ];
1317 
1318  $this->setup->getConnection()->insert($table, $data);
1319  }
1320 
1321  return $this;
1322  }
getAttributeGroupId($entityTypeId, $setId, $groupId)
Definition: EavSetup.php:669
getEntityTypeId($entityTypeId)
Definition: EavSetup.php:267
getAttributeSortOrder($entityTypeId, $setId, $groupId, $sortOrder=null)
Definition: EavSetup.php:1258
getAttributeSetId($entityTypeId, $setId)
Definition: EavSetup.php:409
getAttributeId($entityTypeId, $id)
Definition: EavSetup.php:1177
$table
Definition: trigger.php:14

◆ addEntityType()

addEntityType (   $code,
array  $params 
)

Add an entity type

If already exists updates the entity type with params data

Parameters
string$code
array$params
Returns
$this

Definition at line 184 of file EavSetup.php.

185  {
186  $data = [
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'),
199  ];
200  if (isset($params['entity_type_id'])) {
201  $data['entity_type_id'] = $params['entity_type_id'];
202  }
203 
204  if ($this->getEntityType($code, 'entity_type_id')) {
205  $this->updateEntityType($code, $data);
206  } else {
207  $this->setup->getConnection()->insert(
208  $this->setup->getTable('eav_entity_type'),
209  $data
210  );
211  }
212 
213  if (isset($params['entity_type_id'])) {
214  $this->addAttributeSet($code, $this->_defaultAttributeSetName, null, $params['entity_type_id']);
215  } else {
216  $this->addAttributeSet($code, $this->_defaultAttributeSetName);
217  }
218  $this->addAttributeGroup($code, $this->_defaultGroupName, $this->_generalGroupName);
219 
220  return $this;
221  }
addAttributeGroup($entityTypeId, $setId, $name, $sortOrder=null)
Definition: EavSetup.php:533
getEntityType($id, $field=null)
Definition: EavSetup.php:250
updateEntityType($code, $field, $value=null)
Definition: EavSetup.php:231
addAttributeSet($entityTypeId, $name, $sortOrder=null, $setId=null)
Definition: EavSetup.php:331
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
$code
Definition: info.phtml:12

◆ cleanCache()

cleanCache ( )

Clean cache

Returns
$this

Definition at line 128 of file EavSetup.php.

129  {
130  $this->cache->clean([\Magento\Eav\Model\Cache\Type::CACHE_TAG]);
131  return $this;
132  }

◆ convertToAttributeGroupCode()

convertToAttributeGroupCode (   $groupName)
Parameters
string$groupName
Returns
string
Since
100.1.0

Definition at line 575 of file EavSetup.php.

576  {
577  return trim(preg_replace('/[^a-z0-9]+/', '-', strtolower($groupName)), '-');
578  }

◆ getAllAttributeSetIds()

getAllAttributeSetIds (   $entityTypeId = null)

Get identifiers of all attribute sets

Parameters
int | string | null$entityTypeId
Returns
array

Definition at line 459 of file EavSetup.php.

460  {
461  $select = $this->setup->getConnection()->select()
462  ->from($this->setup->getTable('eav_attribute_set'), 'attribute_set_id');
463 
464  $bind = [];
465  if ($entityTypeId !== null) {
466  $bind['entity_type_id'] = $this->getEntityTypeId($entityTypeId);
467  $select->where('entity_type_id = :entity_type_id');
468  }
469 
470  return $this->setup->getConnection()->fetchCol($select, $bind);
471  }
getEntityTypeId($entityTypeId)
Definition: EavSetup.php:267

◆ getAttribute()

getAttribute (   $entityTypeId,
  $id,
  $field = null 
)

Retrieve Attribute Data By Id or Code

Parameters
int | string$entityTypeId
int | string$id
string$field
Returns
mixed

Definition at line 1128 of file EavSetup.php.

1129  {
1130  $additionalTable = $this->getEntityType($entityTypeId, 'additional_attribute_table');
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);
1135  }
1136 
1137  $mainTable = $this->setup->getTable('eav_attribute');
1138  $setupCache = $this->setup->getSetupCache();
1139  if (!$setupCache->has($mainTable, $entityTypeId, $id)) {
1140  $additionalTable = $this->setup->getTable($additionalTable);
1141  $bind = ['id' => $id, 'entity_type_id' => $entityTypeId];
1142  $select = $this->setup->getConnection()->select()->from(
1143  ['main' => $mainTable]
1144  )->join(
1145  ['additional' => $additionalTable],
1146  'main.attribute_id = additional.attribute_id'
1147  )->where(
1148  "main.{$idField} = :id"
1149  )->where(
1150  'main.entity_type_id = :entity_type_id'
1151  );
1152 
1153  $row = $this->setup->getConnection()->fetchRow($select, $bind);
1154  if (!$row) {
1155  $setupCache->setRow($mainTable, $entityTypeId, $id, []);
1156  } else {
1157  $setupCache->setRow($mainTable, $entityTypeId, $row['attribute_id'], $row);
1158  $setupCache->setRow($mainTable, $entityTypeId, $row['attribute_code'], $row);
1159  }
1160  }
1161 
1162  $row = $setupCache->get($mainTable, $entityTypeId, $id);
1163  if ($field !== null) {
1164  return isset($row[$field]) ? $row[$field] : false;
1165  }
1166 
1167  return $row;
1168  }
getEntityTypeId($entityTypeId)
Definition: EavSetup.php:267
$id
Definition: fieldset.phtml:14
getEntityType($id, $field=null)
Definition: EavSetup.php:250

◆ getAttributeGroup()

getAttributeGroup (   $entityTypeId,
  $setId,
  $id,
  $field = null 
)

Retrieve Attribute Group Data

Parameters
int | string$entityTypeId
int | string$setId
int | string$id
string$field
Returns
mixed

Definition at line 614 of file EavSetup.php.

615  {
616  if (is_numeric($id)) {
617  $searchField = 'attribute_group_id';
618  } else {
620  if (isset($this->defaultGroupIdAssociations[$id])) {
621  $searchField = 'default_id';
622  $id = $this->defaultGroupIdAssociations[$id];
623  } else {
624  $searchField = 'attribute_group_code';
625  }
626  }
627 
628  return $this->setup->getTableRow(
629  'eav_attribute_group',
630  $searchField,
631  $id,
632  $field,
633  'attribute_set_id',
634  $this->getAttributeSetId($entityTypeId, $setId)
635  );
636  }
convertToAttributeGroupCode($groupName)
Definition: EavSetup.php:575
getAttributeSetId($entityTypeId, $setId)
Definition: EavSetup.php:409
$id
Definition: fieldset.phtml:14

◆ getAttributeGroupByCode()

getAttributeGroupByCode (   $entityTypeId,
  $setId,
  $code,
  $field = null 
)

Retrieve Attribute Group Data by Code

Parameters
int | string$entityTypeId
int | string$setId
string$code
string$field
Returns
mixed
Since
100.1.0

Definition at line 648 of file EavSetup.php.

649  {
650  return $this->setup->getTableRow(
651  'eav_attribute_group',
652  'attribute_group_code',
653  $code,
654  $field,
655  'attribute_set_id',
656  $this->getAttributeSetId($entityTypeId, $setId)
657  );
658  }
getAttributeSetId($entityTypeId, $setId)
Definition: EavSetup.php:409
$code
Definition: info.phtml:12

◆ getAttributeGroupCollectionFactory()

getAttributeGroupCollectionFactory ( )

Gets attribute group collection factory

Returns
\Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\Collection

Definition at line 118 of file EavSetup.php.

119  {
120  return $this->attrGroupCollectionFactory->create();
121  }

◆ getAttributeGroupId()

getAttributeGroupId (   $entityTypeId,
  $setId,
  $groupId 
)

Retrieve Attribute Group Id by Id or Name

Parameters
int | string$entityTypeId
int | string$setId
int | string$groupId
Returns
$this
Exceptions
LocalizedException

Definition at line 669 of file EavSetup.php.

670  {
671  if (!is_numeric($groupId)) {
672  $groupId = $this->getAttributeGroup($entityTypeId, $setId, $groupId, 'attribute_group_id');
673  }
674 
675  if (!is_numeric($groupId)) {
677  }
678 
679  if (!is_numeric($groupId)) {
680  throw new LocalizedException(__('The attribute group ID is incorrect. Verify the ID and try again.'));
681  }
682  return $groupId;
683  }
__()
Definition: __.php:13
getDefaultAttributeGroupId($entityType, $attributeSetId=null)
Definition: EavSetup.php:710
getAttributeGroup($entityTypeId, $setId, $id, $field=null)
Definition: EavSetup.php:614

◆ getAttributeGroupSortOrder()

getAttributeGroupSortOrder (   $entityTypeId,
  $setId,
  $sortOrder = null 
)

Retrieve Attribute Group Sort order

Parameters
int | string$entityTypeId
int | string$setId
int$sortOrder
Returns
int

Definition at line 507 of file EavSetup.php.

508  {
509  if (!is_numeric($sortOrder)) {
510  $bind = ['attribute_set_id' => $this->getAttributeSetId($entityTypeId, $setId)];
511  $select = $this->setup->getConnection()->select()->from(
512  $this->setup->getTable('eav_attribute_group'),
513  'MAX(sort_order)'
514  )->where(
515  'attribute_set_id = :attribute_set_id'
516  );
517 
518  $sortOrder = $this->setup->getConnection()->fetchOne($select, $bind) + 1;
519  }
520 
521  return $sortOrder;
522  }
getAttributeSetId($entityTypeId, $setId)
Definition: EavSetup.php:409

◆ getAttributeId()

getAttributeId (   $entityTypeId,
  $id 
)

Retrieve Attribute Id Data By Id or Code

Parameters
int | string$entityTypeId
int | string$id
Returns
int

Definition at line 1177 of file EavSetup.php.

1178  {
1179  if (!is_numeric($id)) {
1180  $id = $this->getAttribute($entityTypeId, $id, 'attribute_id');
1181  }
1182  if (!is_numeric($id)) {
1183  return false;
1184  }
1185  return $id;
1186  }
$id
Definition: fieldset.phtml:14
getAttribute($entityTypeId, $id, $field=null)
Definition: EavSetup.php:1128

◆ getAttributeSet()

getAttributeSet (   $entityTypeId,
  $id,
  $field = null 
)

Retrieve Attribute set data by id or name

Parameters
int | string$entityTypeId
int | string$id
string$field
Returns
mixed

Definition at line 389 of file EavSetup.php.

390  {
391  return $this->setup->getTableRow(
392  'eav_attribute_set',
393  is_numeric($id) ? 'attribute_set_id' : 'attribute_set_name',
394  $id,
395  $field,
396  'entity_type_id',
398  );
399  }
getEntityTypeId($entityTypeId)
Definition: EavSetup.php:267
$id
Definition: fieldset.phtml:14

◆ getAttributeSetId()

getAttributeSetId (   $entityTypeId,
  $setId 
)

Retrieve Attribute Set Id By Id or Name

Parameters
int | string$entityTypeId
int | string$setId
Returns
int
Exceptions
LocalizedException

Definition at line 409 of file EavSetup.php.

410  {
411  if (!is_numeric($setId)) {
412  $setId = $this->getAttributeSet($entityTypeId, $setId, 'attribute_set_id');
413  }
414  if (!is_numeric($setId)) {
415  throw new LocalizedException(__('The attribute set ID is incorrect. Verify the ID and try again.'));
416  }
417 
418  return $setId;
419  }
getAttributeSet($entityTypeId, $id, $field=null)
Definition: EavSetup.php:389
__()
Definition: __.php:13

◆ getAttributeSetSortOrder()

getAttributeSetSortOrder (   $entityTypeId,
  $sortOrder = null 
)

Retrieve Attribute Set Sort order

Parameters
int | string$entityTypeId
int$sortOrder
Returns
int

Definition at line 305 of file EavSetup.php.

306  {
307  if (!is_numeric($sortOrder)) {
308  $bind = ['entity_type_id' => $this->getEntityTypeId($entityTypeId)];
309  $select = $this->setup->getConnection()->select()->from(
310  $this->setup->getTable('eav_attribute_set'),
311  'MAX(sort_order)'
312  )->where(
313  'entity_type_id = :entity_type_id'
314  );
315 
316  $sortOrder = $this->setup->getConnection()->fetchOne($select, $bind) + 1;
317  }
318 
319  return $sortOrder;
320  }
getEntityTypeId($entityTypeId)
Definition: EavSetup.php:267

◆ getAttributesNumberInGroup()

getAttributesNumberInGroup (   $entityTypeId,
  $setId,
  $groupId 
)

Get number of all attributes in group

Parameters
int | string$entityTypeId
int | string$setId
int | string$groupId
Returns
string

Definition at line 740 of file EavSetup.php.

741  {
742  $select = $this->setup->getConnection()->select()->from(
743  $this->setup->getTable('eav_entity_attribute'),
744  ['count' => 'COUNT(*)']
745  )->where(
746  'attribute_group_id = ?',
748  )->where(
749  'entity_type_id = ?',
751  )->where(
752  'attribute_set_id = ?',
753  $setId
754  );
755 
756  return $this->setup->getConnection()->fetchOne($select);
757  }
getAttributeGroupId($entityTypeId, $setId, $groupId)
Definition: EavSetup.php:669

◆ getAttributeSortOrder()

getAttributeSortOrder (   $entityTypeId,
  $setId,
  $groupId,
  $sortOrder = null 
)

Retrieve Attribute Sort Order

Parameters
int | string$entityTypeId
int | string$setId
int | string$groupId
int$sortOrder
Returns
$this

Definition at line 1258 of file EavSetup.php.

1259  {
1260  if (!is_numeric($sortOrder)) {
1261  $bind = ['attribute_group_id' => $this->getAttributeGroupId($entityTypeId, $setId, $groupId)];
1262  $select = $this->setup->getConnection()->select()->from(
1263  $this->setup->getTable('eav_entity_attribute'),
1264  'MAX(sort_order)'
1265  )->where(
1266  'attribute_group_id = :attribute_group_id'
1267  );
1268 
1269  $sortOrder = $this->setup->getConnection()->fetchOne($select, $bind) + 1;
1270  }
1271 
1272  return $sortOrder;
1273  }
getAttributeGroupId($entityTypeId, $setId, $groupId)
Definition: EavSetup.php:669

◆ getAttributeTable()

getAttributeTable (   $entityTypeId,
  $id 
)

Return table name for eav attribute

Parameters
int | string$entityTypeIdEntity Type id or Entity Type code
int | string$idAttribute id or Attribute code
Returns
string

Definition at line 1195 of file EavSetup.php.

1196  {
1197  $entityKeyName = is_numeric($entityTypeId) ? 'entity_type_id' : 'entity_type_code';
1198  $attributeKeyName = is_numeric($id) ? 'attribute_id' : 'attribute_code';
1199 
1200  $bind = ['id' => $id, 'entity_type_id' => $entityTypeId];
1201  $select = $this->setup->getConnection()->select()->from(
1202  ['entity_type' => $this->setup->getTable('eav_entity_type')],
1203  ['entity_table']
1204  )->join(
1205  ['attribute' => $this->setup->getTable('eav_attribute')],
1206  'attribute.entity_type_id = entity_type.entity_type_id',
1207  ['backend_type']
1208  )->where(
1209  "entity_type.{$entityKeyName} = :entity_type_id"
1210  )->where(
1211  "attribute.{$attributeKeyName} = :id"
1212  )->limit(
1213  1
1214  );
1215 
1216  $result = $this->setup->getConnection()->fetchRow($select, $bind);
1217  if ($result) {
1218  $table = $this->setup->getTable($result['entity_table']);
1219  if ($result['backend_type'] != 'static') {
1220  $table .= '_' . $result['backend_type'];
1221  }
1222  return $table;
1223  }
1224 
1225  return false;
1226  }
$id
Definition: fieldset.phtml:14
$table
Definition: trigger.php:14

◆ getDefaultAttributeGroupId()

getDefaultAttributeGroupId (   $entityType,
  $attributeSetId = null 
)

Retrieve Default Attribute Group Id By Entity Type and Attribute Set

Parameters
string | int$entityType
int$attributeSetId
Returns
int

Definition at line 710 of file EavSetup.php.

711  {
713  if (!is_numeric($attributeSetId)) {
715  }
716  $bind = ['attribute_set_id' => $attributeSetId];
717  $select = $this->setup->getConnection()->select()->from(
718  $this->setup->getTable('eav_attribute_group'),
719  'attribute_group_id'
720  )->where(
721  'attribute_set_id = :attribute_set_id'
722  )->order(
723  ['default_id ' . \Magento\Framework\DB\Select::SQL_DESC, 'sort_order']
724  )->limit(
725  1
726  );
727 
728  return $this->setup->getConnection()->fetchOne($select, $bind);
729  }
getEntityTypeId($entityTypeId)
Definition: EavSetup.php:267
getDefaultAttributeSetId($entityType)
Definition: EavSetup.php:479

◆ getDefaultAttributeSetId()

getDefaultAttributeSetId (   $entityType)

Retrieve Default Attribute Set for Entity Type

Parameters
string | int$entityType
Returns
int

Definition at line 479 of file EavSetup.php.

480  {
481  $bind = ['entity_type' => $entityType];
482  if (is_numeric($entityType)) {
483  $where = 'entity_type_id = :entity_type';
484  } else {
485  $where = 'entity_type_code = :entity_type';
486  }
487  $select = $this->setup->getConnection()->select()->from(
488  $this->setup->getTable('eav_entity_type'),
489  'default_attribute_set_id'
490  )->where(
491  $where
492  );
493 
494  return $this->setup->getConnection()->fetchOne($select, $bind);
495  }

◆ getDefaultEntities()

getDefaultEntities ( )

Gets default entities and attributes

Returns
array

Definition at line 1403 of file EavSetup.php.

1404  {
1405  return [];
1406  }

◆ getEntityType()

getEntityType (   $id,
  $field = null 
)

Retrieve Entity Type Data

Parameters
int | string$id
string$field
Returns
mixed

Definition at line 250 of file EavSetup.php.

251  {
252  return $this->setup->getTableRow(
253  'eav_entity_type',
254  is_numeric($id) ? 'entity_type_id' : 'entity_type_code',
255  $id,
256  $field
257  );
258  }
$id
Definition: fieldset.phtml:14

◆ getEntityTypeId()

getEntityTypeId (   $entityTypeId)

Retrieve Entity Type Id By Id or Code

Parameters
int | string$entityTypeId
Returns
int
Exceptions
LocalizedException

Definition at line 267 of file EavSetup.php.

268  {
269  if (!is_numeric($entityTypeId)) {
270  $entityTypeId = $this->getEntityType($entityTypeId, 'entity_type_id');
271  }
272  if (!is_numeric($entityTypeId)) {
273  throw new LocalizedException(__('The entity ID is incorrect. Verify the ID and try again.'));
274  }
275 
276  return $entityTypeId;
277  }
__()
Definition: __.php:13
getEntityType($id, $field=null)
Definition: EavSetup.php:250

◆ getSetup()

getSetup ( )

Gets setup model

Deprecated:
Returns
ModuleDataSetupInterface

Definition at line 108 of file EavSetup.php.

109  {
110  return $this->setup;
111  }

◆ installDefaultGroupIds()

installDefaultGroupIds ( )

Install Default Group Ids

Returns
$this

Definition at line 139 of file EavSetup.php.

140  {
141  $setIds = $this->getAllAttributeSetIds();
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',
147  $defaultGroupCode,
148  'attribute_group_id',
149  'attribute_set_id',
150  $set
151  );
152  if (!$groupId) {
153  $groupId = $this->setup->getTableRow(
154  'eav_attribute_group',
155  'attribute_set_id',
156  $set,
157  'attribute_group_id'
158  );
159  }
160  $this->setup->updateTableRow(
161  'eav_attribute_group',
162  'attribute_group_id',
163  $groupId,
164  'default_id',
166  );
167  }
168  }
169 
170  return $this;
171  }
getAllAttributeSetIds($entityTypeId=null)
Definition: EavSetup.php:459

◆ installEntities()

installEntities (   $entities = null)

Install entities

Parameters
array$entities
Returns
$this @SuppressWarnings(PHPMD.CyclomaticComplexity) @SuppressWarnings(PHPMD.NPathComplexity)

Definition at line 1416 of file EavSetup.php.

1417  {
1418  $this->cleanCache();
1419 
1420  if ($entities === null) {
1421  $entities = $this->getDefaultEntities();
1422  }
1423 
1424  foreach ($entities as $entityName => $entity) {
1425  $this->addEntityType($entityName, $entity);
1426 
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'] : '';
1430 
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;
1436  } elseif ('_' === $attr['backend'][0]) {
1437  $attr['backend'] = $backendPrefix . $attr['backend'];
1438  }
1439  }
1440  if (!empty($attr['frontend'])) {
1441  if ('_' === $attr['frontend']) {
1442  $attr['frontend'] = $frontendPrefix;
1443  } elseif ('_' === $attr['frontend'][0]) {
1444  $attr['frontend'] = $frontendPrefix . $attr['frontend'];
1445  }
1446  }
1447  if (!empty($attr['source'])) {
1448  if ('_' === $attr['source']) {
1449  $attr['source'] = $sourcePrefix;
1450  } elseif ('_' === $attr['source'][0]) {
1451  $attr['source'] = $sourcePrefix . $attr['source'];
1452  }
1453  }
1454 
1455  $this->addAttribute($entityName, $attrCode, $attr);
1456  }
1457  }
1458  $this->setDefaultSetToEntityType($entityName);
1459  }
1460 
1461  return $this;
1462  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$attr
Definition: text.phtml:8
setDefaultSetToEntityType($entityType, $attributeSet='Default')
Definition: EavSetup.php:445
addAttribute($entityTypeId, $code, array $attr)
Definition: EavSetup.php:819
$entity
Definition: element.phtml:22
addEntityType($code, array $params)
Definition: EavSetup.php:184

◆ removeAttribute()

removeAttribute (   $entityTypeId,
  $code 
)

Remove Attribute

Parameters
int | string$entityTypeId
int | string$code
Returns
$this

Definition at line 1235 of file EavSetup.php.

1236  {
1237  $mainTable = $this->setup->getTable('eav_attribute');
1239  if ($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'])) {
1243  $setupCache->remove($mainTable, $attribute['entity_type_id'], $attribute['attribute_code']);
1244  }
1245  }
1246  return $this;
1247  }
getAttribute($entityTypeId, $id, $field=null)
Definition: EavSetup.php:1128
$code
Definition: info.phtml:12

◆ removeAttributeGroup()

removeAttributeGroup (   $entityTypeId,
  $setId,
  $id 
)

Remove Attribute Group By Id or Name

Parameters
int | string$entityTypeId
int | string$setId
int | string$id
Returns
$this

Definition at line 693 of file EavSetup.php.

694  {
695  $this->setup->deleteTableRow(
696  'eav_attribute_group',
697  'attribute_group_id',
698  $this->getAttributeGroupId($entityTypeId, $setId, $id)
699  );
700  return $this;
701  }
getAttributeGroupId($entityTypeId, $setId, $groupId)
Definition: EavSetup.php:669
$id
Definition: fieldset.phtml:14

◆ removeAttributeSet()

removeAttributeSet (   $entityTypeId,
  $id 
)

Remove Attribute Set

Parameters
int | string$entityTypeId
int | string$id
Returns
$this

Definition at line 428 of file EavSetup.php.

429  {
430  $this->setup->deleteTableRow(
431  'eav_attribute_set',
432  'attribute_set_id',
434  );
435  return $this;
436  }
getAttributeSetId($entityTypeId, $setId)
Definition: EavSetup.php:409
$id
Definition: fieldset.phtml:14

◆ removeEntityType()

removeEntityType (   $id)

Remove entity type by Id or Code

Parameters
int | string$id
Returns
$this

Definition at line 285 of file EavSetup.php.

286  {
287  if (is_numeric($id)) {
288  $this->setup->deleteTableRow('eav_entity_type', 'entity_type_id', $id);
289  } else {
290  $this->setup->deleteTableRow('eav_entity_type', 'entity_type_code', (string)$id);
291  }
292 
293  return $this;
294  }
$id
Definition: fieldset.phtml:14

◆ setDefaultSetToEntityType()

setDefaultSetToEntityType (   $entityType,
  $attributeSet = 'Default' 
)

Set Default Attribute Set to Entity Type

Parameters
int | string$entityType
string$attributeSet
Returns
$this

Definition at line 445 of file EavSetup.php.

446  {
449  $this->updateEntityType($entityTypeId, 'default_attribute_set_id', $setId);
450  return $this;
451  }
getEntityTypeId($entityTypeId)
Definition: EavSetup.php:267
getAttributeSetId($entityTypeId, $setId)
Definition: EavSetup.php:409
updateEntityType($code, $field, $value=null)
Definition: EavSetup.php:231

◆ updateAttribute()

updateAttribute (   $entityTypeId,
  $id,
  $field,
  $value = null,
  $sortOrder = null 
)

Update Attribute data and Attribute additional data

Parameters
int | string$entityTypeId
int | string$id
string | array$field
mixed$value
int$sortOrder
Returns
$this

Definition at line 954 of file EavSetup.php.

955  {
956  $this->_updateAttribute($entityTypeId, $id, $field, $value, $sortOrder);
957  $this->_updateAttributeAdditionalData($entityTypeId, $id, $field, $value);
958  return $this;
959  }
$id
Definition: fieldset.phtml:14
$value
Definition: gender.phtml:16

◆ updateAttributeGroup()

updateAttributeGroup (   $entityTypeId,
  $setId,
  $id,
  $field,
  $value = null 
)

Update Attribute Group Data

Parameters
int | string$entityTypeId
int | string$setId
int | string$id
string$field
mixed$value
Returns
$this

Definition at line 590 of file EavSetup.php.

591  {
592  $this->setup->updateTableRow(
593  'eav_attribute_group',
594  'attribute_group_id',
595  $this->getAttributeGroupId($entityTypeId, $setId, $id),
596  $field,
597  $value,
598  'attribute_set_id',
599  $this->getAttributeSetId($entityTypeId, $setId)
600  );
601 
602  return $this;
603  }
getAttributeGroupId($entityTypeId, $setId, $groupId)
Definition: EavSetup.php:669
getAttributeSetId($entityTypeId, $setId)
Definition: EavSetup.php:409
$id
Definition: fieldset.phtml:14
$value
Definition: gender.phtml:16

◆ updateAttributeSet()

updateAttributeSet (   $entityTypeId,
  $id,
  $field,
  $value = null 
)

Update attribute set data

Parameters
int | string$entityTypeId
int$id
string$field
mixed$value
Returns
$this

Definition at line 367 of file EavSetup.php.

368  {
369  $this->setup->updateTableRow(
370  'eav_attribute_set',
371  'attribute_set_id',
373  $field,
374  $value,
375  'entity_type_id',
377  );
378  return $this;
379  }
getEntityTypeId($entityTypeId)
Definition: EavSetup.php:267
getAttributeSetId($entityTypeId, $setId)
Definition: EavSetup.php:409
$id
Definition: fieldset.phtml:14
$value
Definition: gender.phtml:16

◆ updateEntityType()

updateEntityType (   $code,
  $field,
  $value = null 
)

Update entity row

Parameters
string$code
string$field
string$value
Returns
$this

Definition at line 231 of file EavSetup.php.

232  {
233  $this->setup->updateTableRow(
234  'eav_entity_type',
235  'entity_type_id',
236  $this->getEntityTypeId($code),
237  $field,
238  $value
239  );
240  return $this;
241  }
getEntityTypeId($entityTypeId)
Definition: EavSetup.php:267
$value
Definition: gender.phtml:16
$code
Definition: info.phtml:12

The documentation for this class was generated from the following file: