我一直在抓取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()
调用失败并导致此错误。有没有人知道为什么会发生这种情况,以及我如何才能获得颜色选项?
谢谢!
发布于 2010-11-10 07:44:29
它看起来像是你自己初始化属性,而不是使用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
模型有很多有用的方法,可以在您的开发中使用,所以请不要犹豫研究这个模型。
发布于 2013-04-17 08:31:15
如果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);
发布于 2012-06-19 20:19:52
$attribute = Mage::getModel('eav/config')->getAttribute('customer','cateinterest');
$options = $attribute->getSource()->getAllOptions();
https://stackoverflow.com/questions/3994224
复制相似问题