Attribute Set Structure
eav_attribute_set Table
CREATE TABLE eav_attribute_set (
attribute_set_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'Attribute Set ID',
entity_type_id SMALLINT UNSIGNED NOT NULL COMMENT 'Entity Type ID',
attribute_set_name VARCHAR(255) DEFAULT NULL COMMENT 'Attribute Set Name',
sort_order SMALLINT NOT NULL DEFAULT 0 COMMENT 'Sort Order',
PRIMARY KEY (attribute_set_id),
INDEX IDX_ENTITY_TYPE_ID (entity_type_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='EAV Attribute Set';
-- Default attribute sets
SELECT * FROM eav_attribute_set WHERE entity_type_id = 4;
+---------------------+----------------+----------------------+------------+
| attribute_set_id | entity_type_id | attribute_set_name | sort_order |
+---------------------+----------------+----------------------+------------+
| 1 | 4 | Default | 0 |
| 4 | 4 | Electronics | 10 |
| 5 | 4 | Clothing | 20 |
| 6 | 4 | Books | 30 |
+---------------------+----------------+----------------------+------------+
eav_attribute_group Table
CREATE TABLE eav_attribute_group (
attribute_group_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'Attribute Group ID',
attribute_set_id SMALLINT UNSIGNED NOT NULL COMMENT 'Attribute Set ID',
attribute_group_name VARCHAR(255) DEFAULT NULL COMMENT 'Attribute Group Name',
sort_order SMALLINT NOT NULL DEFAULT 0 COMMENT 'Sort Order',
default_id SMALLINT UNSIGNED DEFAULT NULL COMMENT 'Default ID',
PRIMARY KEY (attribute_group_id),
INDEX IDX_ATTRIBUTE_SET_ID (attribute_set_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='EAV Attribute Group';
-- Groups in Default set
SELECT * FROM eav_attribute_group WHERE attribute_set_id = 4 ORDER BY sort_order;
+---------------------+---------------------+-----------------------+------------+
| attribute_group_id | attribute_set_id | attribute_group_name | sort_order |
+---------------------+---------------------+-----------------------+------------+
| 1 | 4 | General | 10 |
| 2 | 4 | Prices | 20 |
| 3 | 4 | Images | 30 |
| 4 | 4 | SEO | 40 |
| 5 | 4 | Design | 50 |
+---------------------+---------------------+-----------------------+------------+
Attribute to Set Assignment
eav_entity_attribute Table
CREATE TABLE eav_entity_attribute (
entity_attribute_id INT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'Entity Attribute ID',
entity_type_id SMALLINT UNSIGNED NOT NULL COMMENT 'Entity Type ID',
attribute_set_id SMALLINT UNSIGNED NOT NULL COMMENT 'Attribute Set ID',
attribute_group_id SMALLINT UNSIGNED NOT NULL COMMENT 'Attribute Group ID',
attribute_id SMALLINT UNSIGNED NOT NULL COMMENT 'Attribute ID',
sort_order SMALLINT NOT NULL DEFAULT 0 COMMENT 'Sort Order',
PRIMARY KEY (entity_attribute_id),
INDEX IDX_ATTRIBUTE_SET_ID (attribute_set_id),
INDEX IDX_ATTRIBUTE_ID (attribute_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='EAV Entity Attribute';
-- Attributes in Default set, General group
SELECT
ea.sort_order,
a.attribute_code,
a.frontend_label,
ag.attribute_group_name
FROM eav_entity_attribute ea
JOIN eav_attribute a ON a.attribute_id = ea.attribute_id
JOIN eav_attribute_group ag ON ag.attribute_group_id = ea.attribute_group_id
WHERE ea.attribute_set_id = 4 AND ea.attribute_group_id = 1
ORDER BY ea.sort_order;
+------------+------------------+-----------------+-----------------------+
| sort_order | attribute_code | frontend_label | attribute_group_name |
+------------+------------------+-----------------+-----------------------+
| 1 | name | Name | General |
| 2 | description | Description | General |
| 3 | short_description| Short Description| General |
| 4 | sku | SKU | General |
| 5 | status | Status | General |
| 6 | visibility | Visibility | General |
+------------+------------------+-----------------+-----------------------+
Assignment Relationships
eav_attribute_set (1)
├── eav_attribute_group (N)
│ ├── sort_order
│ └── attribute_group_name
└── eav_entity_attribute (N)
├── attribute_set_id
├── attribute_group_id
├── attribute_id
└── sort_order
Creating Attribute Sets
Via Setup Script
<?php
namespace Vendor\Module\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
public function __construct(
private \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory
) {}
public function install(
ModuleDataSetupInterface $setup,
ModuleContextInterface $context
) {
$setup->startSetup();
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
// Create attribute set
$eavSetup->addAttributeSet(
\Magento\Catalog\Model\Product::ENTITY,
'My Custom Set',
['sort_order' => 100]
);
// Get attribute set ID
$setId = $eavSetup->getAttributeSetId(
\Magento\Catalog\Model\Product::ENTITY,
'My Custom Set'
);
// Create attribute group
$eavSetup->addAttributeGroup(
\Magento\Catalog\Model\Product::ENTITY,
$setId,
'My Custom Group',
10
);
// Get group ID
$groupId = $eavSetup->getAttributeGroupId(
\Magento\Catalog\Model\Product::ENTITY,
$setId,
'My Custom Group'
);
// Assign attribute to set and group
$eavSetup->addAttributeToSet(
\Magento\Catalog\Model\Product::ENTITY,
$setId,
$groupId,
'custom_attribute'
);
$setup->endSetup();
}
}
Via Data Patch
<?php
namespace Vendor\Module\Setup\Patch\Data;
use Magento\Framework\Setup\Patch\DataPatchInterface;
class CreateAttributeSet implements DataPatchInterface
{
public function __construct(
private \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory,
private \Magento\Framework\Setup\ModuleDataSetupInterface $setup
) {}
public function install(): void
{
$this->setup->startSetup();
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->setup]);
$eavSetup->addAttributeSet(
\Magento\Catalog\Model\Product::ENTITY,
'Custom Electronics',
['sort_order' => 200]
);
$this->setup->endSetup();
}
public function getAliases(): array { return []; }
public function getDependencies(): array { return []; }
}
Managing Attribute Sets
Admin Management
Admin > Stores > Attributes > Attribute Set
1. Create New Set:
- Set Name: Custom Set
- Based On: Default
2. Add Group:
- Click 'Add New' in Groups section
- Name: My Group
3. Assign Attributes:
- Drag attributes from 'Unassigned' to group
- Or click '+' to add to group
4. Reorder:
- Drag attributes within group
- Drag groups to reorder
Programmatic Management
// Get attribute set
$attributeSet = $objectManager->get(
\Magento\Eav\Model\Entity\Attribute\SetFactory::class
)->create()->load($setId);
// Get groups in set
$groups = $attributeSet->getGroups();
// Get attributes in group
$attributes = $group->getAttributes();
// Remove attribute from set
$eavSetup->removeAttributeFromSet(
\Magento\Catalog\Model\Product::ENTITY,
$setId,
$attributeId
);
// Move attribute to different group
$eavSetup->addAttributeToSet(
\Magento\Catalog\Model\Product::ENTITY,
$setId,
$newGroupId,
$attributeCode
);
Assigning Products to Sets
// Assign product to attribute set
$product->setAttributeSetId($newSetId);
$product->save();
// Get available attribute sets
$collection = $objectManager->get(
\Magento\Eav\Model\Entity\Attribute\SetFactory::class
)->create()->getCollection();
foreach ($collection as $set) {
echo $set->getAttributeSetName();
}
Best Practices
1. Use descriptive set names (Electronics, Clothing)
2. Organize attributes into logical groups
3. Set appropriate sort orders
4. Don't create too many sets (5-10 max)
5. Reuse attributes across sets when possible
6. Test attribute set assignment before production
Quiz
1. What is the purpose of attribute sets?
2. What is an attribute group?
3. How do you assign an attribute to a set?
Flashcards
Question
What is an attribute set?
Click to reveal answer
Answer
A collection of attributes grouped for a specific product type
Question
What is an attribute group?
Click to reveal answer
Answer
A section within an attribute set that organizes related attributes
Question
Where is set-group-attribute assignment?
Click to reveal answer
Answer
eav_entity_attribute table linking set, group, and attribute
Question
How to create an attribute set?
Click to reveal answer
Answer
$eavSetup->addAttributeSet(entity, name, options)
Question
How to assign attribute to set?
Click to reveal answer
Answer
$eavSetup->addAttributeToSet(entity, setId, groupId, attributeCode)
Revision Notes
Key Takeaways
- 1. Attribute sets group attributes for different product types
- 2. Attribute groups organize attributes within a set
- 3. eav_entity_attribute links sets, groups, and attributes
- 4. Default set exists for each entity type
- 5. Use addAttributeSet() and addAttributeToSet() programmatically
Interview Tips
- • Explain how attribute sets organize product attributes
- • Discuss the relationship between sets, groups, and attributes
- • Describe creating custom attribute sets programmatically
Cheat Sheet
Attribute Sets:
eav_attribute_set → attribute_set_id, entity_type_id, attribute_set_name
Attribute Groups:
eav_attribute_group → attribute_group_id, attribute_set_id, attribute_group_name
Assignment:
eav_entity_attribute → attribute_set_id, attribute_group_id, attribute_id
Programmatic:
$eavSetup->addAttributeSet($entity, $name, $options)
$eavSetup->addAttributeGroup($entity, $setId, $groupName, $sortOrder)
$eavSetup->addAttributeToSet($entity, $setId, $groupId, $attributeCode)