【发布时间】:2011-09-26 07:20:02
【问题描述】:
我如何在 Magento 中通过 attribute_code 获得 Attribute model(来自:eav_attribute 表)?
通知:
- 我不在乎 entity_type 是什么。
非常感谢。
【问题讨论】:
标签: magento
我如何在 Magento 中通过 attribute_code 获得 Attribute model(来自:eav_attribute 表)?
通知:
- 我不在乎 entity_type 是什么。
非常感谢。
【问题讨论】:
标签: magento
您必须知道entity_type,因为您可以为不同的实体拥有相同的attribute_code。所以要获取属性模型:
$attributeModel = Mage::getModel('eav/entity_attribute')->loadByCode($entity_type, $attributeCode);
$entity_type参数可以是numeric(id直接),string(例如'catalog_product'或Mage_Catalog_Model_Product::ENTITY)或者它可以是模型的实例Mage_Eav_Model_Entity_Type
【讨论】:
也许你可以通过过滤集合来读取属性
Mage::getModel('eav/entity_attribute')->getCollection()->addFieldToFilter('attribute_code', array('in' => $codes) )
由于我需要通过代码从产品中获取属性,所以我这样做:
$codes = (array) $codes;
$res = array_intersect_key($this->getAttributes(), array_flip($codes));
$codes 是一个属性代码数组 范围:扩展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();
【讨论】: