letsanov
letsanov

Reputation: 201

Get all existing values of attribute in the eav table

I have the attribute "country" which is varchar, it is set in every product that I have. I need to have a list of all the values that are set for this attribute thought all the products. Basically what I want to do is the equivalent of this query:

SELECT `value` FROM `catalog_product_entity_varchar` WHERE attribute_id=147

How to do this the magento way?

The thing I am doing at the moment is to get the collection of all the products and loop through them, which is not an option.

Upvotes: 1

Views: 945

Answers (2)

Sunil Patel
Sunil Patel

Reputation: 536

Check below code:-

$attribute = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'color');
if ($attribute->usesSource()) {
    $options = $attribute->getSource()->getAllOptions(false);
}

Upvotes: 2

S P
S P

Reputation: 4643

What about this?

 $collection = Mage::getModel('catalog/product')->getCollection();
 $collection->addAttributeToSelect('sku');  
 $collection->addAttributeToSelect('country');

 foreach ($collection as $product) { 
    // ... Your stuff here
 }

Upvotes: 1

Related Questions