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
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
-
-
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
-
7version using constants: $_attr = Mage::getModel('eav/entity_attribute')->loadByCode(Mage_Catalog_Model_Product::ENTITY, $attributeCode); Commented Jun 17, 2014 at 16:19
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
$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'];
$attributeModel = Mage::getModel('eav/entity_attribute')->loadByCode(1, 'is_approved'); echo $attributeModel->getAttributeId();
-
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.– user3141031Commented Dec 9, 2014 at 2:51