我一直在从Magento中获取属性选项,如下所示:
<?php
if ($attribute->usesSource()) {
$options = $attribute->getSource()->getAllOptions(false);
}
?>
它一直工作正常,直到我试图获取内置'color'属性的选项 - 我收到以下错误:
PHP Fatal error: Call to a member function setAttribute() on a non-object in app/code/core/Mage/Eav/Model/Entity/Attribute/Abstract.php on line 374
似乎getSource()
调用失败并导致此错误。有谁知道为什么会发生这种情况以及我如何获得颜色选项?
谢谢!
看起来你自己初始化属性,而不是使用Magento属性初始化过程:
Mage::getSingleton('eav/config')
->getAttribute($entityType, $attributeCode)
因为1.4.x Magento具有针对目录和客户模型的单独属性模型,并且catalog_product
的默认源模型的定义现在从EAV属性模型(Mage_Eav_Model_Entity_Attribute
)移动到目录一(Mage_Catalog_Model_Resource_Eav_Attribute
)。
因此,某些目录属性不适用于EAV属性模型。特别是那些使用Mage_Eav_Model_Entity_Attribute_Source_Table
但没有明确定义它的颜色(颜色,制造商等)。
以下代码段应完全适用于您的安装:
$attribute = Mage::getSingleton('eav/config')
->getAttribute(Mage_Catalog_Model_Product::ENTITY, 'color');
if ($attribute->usesSource()) {
$options = $attribute->getSource()->getAllOptions(false);
}
顺便说一句Mage_Eav_Model_Config
模型有很多有用的方法,可以用于你的开发,所以不要犹豫,看看这个模型。
如果resource_model为空,则上述代码不起作用。以下代码片段完成了这项工作:
$attribute = Mage::getModel('eav/entity_attribute')->loadByCode(Mage_Catalog_Model_Product::ENTITY, 'YOUR_ATTRIBUTE_CODE');
/** @var $attribute Mage_Eav_Model_Entity_Attribute */
$valuesCollection = Mage::getResourceModel('eav/entity_attribute_option_collection')
->setAttributeFilter($attribute->getId())
->setStoreFilter(0, false);
$attribute = Mage::getModel('eav/config')->getAttribute('customer','cateinterest');
$options = $attribute->getSource()->getAllOptions();
<?php
//Possible color value
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'color'); //"color" is the attribute_code
$allOptions = $attribute->getSource()->getAllOptions(true, true);
foreach ($allOptions as $instance) {
$id = $instance['value']; //id of the option
$value = $instance['label']; //Label of the option
很抱歉答案不完整,但请查看数据库,特别是在backend_model
专栏中。在设置此字段以匹配此方面的某些系统字段之前,我似乎记得遇到同样的问题。