20

How could I get Attribute model (from: eav_attribute table) by attribute_code in Magento?
Notice:
- I don't care what is entity_type.
Thank you so much.

4 Answers 4

55

You have to know entity_type because you can have the same attribute_code for different entities. So to get attribute model:

$attributeModel = Mage::getModel('eav/entity_attribute')->loadByCode($entity_type, $attributeCode);

$entity_type parameter can be numeric (id directly), string (for example 'catalog_product' or Mage_Catalog_Model_Product::ENTITY) or it can be instance of model Mage_Eav_Model_Entity_Type

3
  • Thanks. I will try and confirm the result to you.
    – vietean
    Commented Sep 26, 2011 at 13:38
  • I used your code above to find out if short_description is 'Visible on Product View Page on Front-end': $attributeShortDescription = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', 'short_description'); $attributeShortDescription->getIsVisibleOnFront(); Commented Mar 27, 2014 at 15:29
  • 7
    version using constants: $_attr = Mage::getModel('eav/entity_attribute')->loadByCode(Mage_Catalog_Model_Product::ENTITY, $attributeCode);
    – Steve Ross
    Commented Jun 17, 2014 at 16:19
3

Perhaps you can read the attributes by filter the collection

   Mage::getModel('eav/entity_attribute')->getCollection()->addFieldToFilter('attribute_code', array('in' => $codes) )

Since I need the attributes from the Product by code, I do it like this:

    $codes = (array) $codes; 
    $res = array_intersect_key($this->getAttributes(), array_flip($codes));

$codes is an attribute_code-array Scope: extended Mage_Catalog_Model_Product

3
$attribute_code = "flat_printing_quantity"; 
$attribute_details =
Mage::getSingleton("eav/config")->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attribute_code);
$attribute = $attribute_details->getData();
echo $attribute['attribute_id'];
1

$attributeModel = Mage::getModel('eav/entity_attribute')->loadByCode(1, 'is_approved'); echo $attributeModel->getAttributeId();

1
  • Not only does this have data which is specific to only one of many scenarios, but it also echoes the accepted answer, the code is not formatted and there is no explanation.
    – user3141031
    Commented Dec 9, 2014 at 2:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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