Question

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.

Was it helpful?

Solution

        $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');

OTHER TIPS

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` 

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top