balanv
balanv

Reputation: 10898

Get attributes and its values using single Collection in Magento?

How to get attributes and its values using single Collection in Magento? Right now i am using below

$attributesInfo = Mage::getResourceModel('eav/entity_attribute_collection')
                  ->setEntityTypeFilter(4)
                  ->addFieldToFilter('frontend_input','multiselect')
                  ->addSetInfo()
                  ->getData(); 

to get attribute and below code to get attribute value


$product = Mage::getModel('catalog/product');
$collection = Mage::getResourceModel('eav/entity_attribute_collection')
                ->setEntityTypeFilter($product->getResource()->getTypeId())
                ->addFieldToFilter('attribute_code', $attributeName);

My attributes code out put is like below

 
  Color :
   Black
   Blue
   Green

 Brand :
   Hp
   Dell
   Apple

 Size :
  12
  14
  16

 

Thanks,

Balan

Upvotes: 3

Views: 11506

Answers (1)

Vinai
Vinai

Reputation: 14182

How about this:

$attributes = Mage::getSingleton('eav/config')
    ->getEntityType(Mage_Catalog_Model_Product::ENTITY)
    ->getAttributeCollection()
    ->addFieldToFilter('source_model', array('notnull' => true))
    ->addSetInfo();

foreach ($attributes as $attribute)
{
    echo "{$attribute->getFrontendLabel()}:\n";
    foreach ($attribute->getSource()->getAllOptions() as $option)
    {
        echo "  {$option['label']}\n";
    }
    echo "\n";
}

By using the eav/config and eav/entity_type chances are you will be reusing an already loaded collection, which of course is more efficient then reloading the same data in new collections.

EDIT: Updated the answer to include the attribute collection filter suggested by Aleksandr Ryabov.

Upvotes: 18

Related Questions