How can I get a collection with the attribute sets that contain a certain attribute?
I want to be able do to this from a single collection walk (on single select), to avoid loading each attribute set and checking the attributes inside.
I know this seams like standard stuff, but I'm a bit slow these days.
4 Answers
$attributeId = 96;
$collection = Mage::getModel('eav/entity_attribute_set')
->getCollection()
->addFieldToFilter('eav_entity_attribute.attribute_id', $attributeId)
->addFieldToFilter('eav_entity_type.entity_type_code', Mage_Catalog_Model_Product::ENTITY)
->join(array('eav_entity_type'=> 'eav/entity_type'), 'eav_entity_type.entity_type_id=main_table.entity_type_id','entity_type_code')
->join(array('eav_entity_attribute'=> 'eav/entity_attribute'), 'eav_entity_attribute.attribute_set_id=main_table.attribute_set_id', 'attribute_id');
I found this right before @mageUz wrote the perfect answer. It's fast if you need only the attribute set ids. Maybe someone else needs it.
$attributeId = 123;
$setInfo = Mage::getResourceModel('eav/entity_attribute_set')
->getSetInfo(array($attributeId));
$attributeSetIds = array_keys($setInfo[$attributeId]);
I don't know how to do it with Magento objects but a direct query would work just as well I guess
SELECT `eav_entity_attribute`.`attribute_set_id`
FROM `eav_attribute` , `eav_entity_attribute`
WHERE `eav_attribute`.`attribute_code` = 'cost'
AND `eav_attribute`.`attribute_id` = `eav_entity_attribute`.`attribute_id`
-
Thanks. I found that table also. I guess I have to write my own resource model in order to use this query. +1, but I don't accept the answer yet. I'm hoping for a
Magento
solution– Marius ♦Sep 18, 2013 at 13:15 -
You can use below code to get all attribute of particular attribute set.
$data = Mage::getResourceModel('catalog/product_attribute_collection')
->setAttributeSetFilter(9) // Add attribute set ID
->load();
-
Thanks, but I think you misunderstood the question. I want the other way around. All ATTRIBUTE SETS that contain a certain ATTRIBUTE.– Marius ♦Sep 18, 2013 at 12:22
-
sorry for the inconvenience. But I'll try to find magento solution. Sep 18, 2013 at 13:54
-