Get Attribute model by attribute_code in Magento
How could I get Attribute model
(from: eav_attribute
table) by attribute_co开发者_如何学Cde
in Magento?
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
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();
精彩评论