I need to find the attribute_id values of the attributes image, small_image and thumbnail. I know them for my database - 85, 86 and 87, but I need to make my query dynamic, instead of with hard-coded values. What I struggle with is to find in which table the attributes are stored. I checked catalog_product_attribute but there is no column with the name/code of the attributes.

I need to acquire them as an SQL query, not as Mage::... PHP code. A sample SQL query code or any other guidance will be very helpful.

$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', $attributeCode);
$id = $attribute->getId();

if $id is null then the attribute does not exist.
It works the same for getting category attributes, customer attributes and customer address attributes.

$attribute = Mage::getModel('eav/config')->getAttribute('catalog_category', 'is_anchor');
$attribute = Mage::getModel('eav/config')->getAttribute('customer', 'gender');
$attribute = Mage::getModel('eav/config')->getAttribute('customer_address', 'postcode');

Apparently I can't read. I missed the line that stated sql query.
Here is a query also:

    eav_attribute e
    eav_entity_type t 
ON e.entity_type_id = t.entity_type_id
    e.attribute_code = 'image' AND
    t.entity_type_code = 'catalog_product'
  • I went with an easier solution: SELECT attribute_id FROM eav_attribute WHERE attribute_code = 'image' AND frontend_input = 'media_image', but thanks for showing me that I have to look in eav_attribute table. :) – Syspect Feb 11 '14 at 16:19
  • 2
    Your approach is risky because there could be an other attribute with the same code for a different entity – Marius Feb 11 '14 at 18:54

I used this codes for getting the attribute ID in eav_attribute tables

$attribute_code = 'image';

$eavCollections = Mage::getModel('eav/entity_attribute')->getCollection()
                    ->addFieldToFilter('attribute_code', array('eq'=> $attribute_code ));

                foreach($eavCollections as $eavCollection){
                   $attribute_id = $eavCollection['attribute_id']; //get the attribute id
                   echo $attribute_id;

Short and helpful:

Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product', 'color');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.