How can I obtain the list of all EAV attributes of a specific entity (by its entity_type_id
) via SQL?
2 Answers
EAV attributes in the eav_attribute
table are directly linked to the entity_type
table. So all attributes can be revealed by a left-join query across those two tables:
-- show all eav attributes of an eav entity by entity_type_id
SET @entity_type_id = 10;
SELECT entity_type.entity_type_id, entity_type.entity_type_code
, entity_type.entity_model
, attribute.attribute_id, attribute.attribute_code, attribute.backend_type
FROM eav_entity_type entity_type
LEFT JOIN eav_attribute attribute
ON attribute.entity_type_id = entity_type.entity_type_id
WHERE entity_type.entity_type_id = @entity_type_id;
Example output:
+----------------+------------------+-----------------------+--------------+----------------+--------------+
| entity_type_id | entity_type_code | entity_model | attribute_id | attribute_code | backend_type |
+----------------+------------------+-----------------------+--------------+----------------+--------------+
| 10 | mcd_comment | mcd_meeting05/comment | 246 | comment | text |
| 10 | mcd_comment | mcd_meeting05/comment | 249 | created_at | static |
| 10 | mcd_comment | mcd_meeting05/comment | 245 | customer_id | int |
| 10 | mcd_comment | mcd_meeting05/comment | 244 | guest_email | varchar |
| 10 | mcd_comment | mcd_meeting05/comment | 243 | guest_name | varchar |
| 10 | mcd_comment | mcd_meeting05/comment | 247 | page | varchar |
| 10 | mcd_comment | mcd_meeting05/comment | 251 | product_id | static |
| 10 | mcd_comment | mcd_meeting05/comment | 248 | store_id | static |
| 10 | mcd_comment | mcd_meeting05/comment | 250 | updated_at | static |
+----------------+------------------+-----------------------+--------------+----------------+--------------+
Related non-SQL question:
Please use bellow code
$entityTypeID = 'customer'; //customer,customer_address,catalog_category,catalog_product
$entityType = Mage::getModel('eav/config')->getEntityType($entityTypeID);
$attributes = Mage::getResourceModel($entityType->getEntityAttributeCollection())
->getItems();
foreach($attributes as $attribute) {
print_r($attribute);
}
SQL
select * from eav_attribute where entity_type_id IN (select entity_type_id from eav_entity_type where entity_type_code = 'customer')
-
I was concerned about a Q&A of SQL only. For Magento API PHP code I've referenced an existing question already. The subquery might be handy on quick first sight but lacks the possibility to obtain information from tha
eav_entity_type
table IMHO.– hakreCommented Nov 4, 2015 at 10:58