Chr*_*tte 20 php attributes magento multi-select
我一直在从Magento中获取属性选项,如下所示:
<?php
if ($attribute->usesSource()) {
$options = $attribute->getSource()->getAllOptions(false);
}
?>
Run Code Online (Sandbox Code Playgroud)
它一直工作正常,直到我试图获取内置'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
Run Code Online (Sandbox Code Playgroud)
看来getSource()呼叫失败并导致此错误.有谁知道为什么会发生这种情况以及我如何获得颜色选项?
谢谢!
Iva*_*nyi 62
看起来你自己初始化属性,而不是使用Magento属性初始化过程:
Mage::getSingleton('eav/config')
->getAttribute($entityType, $attributeCode)
Run Code Online (Sandbox Code Playgroud)
因为1.4.x Magento具有针对目录和客户模型的单独属性模型,并且catalog_product现在将默认源模型的定义从EAV属性model(Mage_Eav_Model_Entity_Attribute)移动到目录one(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);
}
Run Code Online (Sandbox Code Playgroud)
顺便说一下,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);
Run Code Online (Sandbox Code Playgroud)
小智 5
$attribute = Mage::getModel('eav/config')->getAttribute('customer','cateinterest');
$options = $attribute->getSource()->getAllOptions();
Run Code Online (Sandbox Code Playgroud)