php 从 Magento 获取属性选项列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3994224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get a list of attribute options from Magento
提问by Chris Forrette
I've been grabbing attribute options from Magento like so:
我一直在从 Magento 中获取属性选项,如下所示:
<?php
if ($attribute->usesSource()) {
$options = $attribute->getSource()->getAllOptions(false);
}
?>
It's been working fine until I tried to get the options for the built in 'color' attribute -- I got the following error:
在我尝试获取内置 '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
It would appear that the getSource()
call fails and causes this error. Does anyone know why this happens and how I can get color options out?
看起来getSource()
调用失败并导致此错误。有谁知道为什么会发生这种情况以及如何获得颜色选项?
Thanks!
谢谢!
回答by Ivan Chepurnyi
It looks like that you initialize attribute by yourself, instead of using Magento attribute initialization process:
看起来是你自己初始化属性,而不是使用Magento的属性初始化过程:
Mage::getSingleton('eav/config')
->getAttribute($entityType, $attributeCode)
Because since 1.4.x Magento has separate attribute models for catalog and customers model and definition of default source model for catalog_product
now is moved from EAV attribute model (Mage_Eav_Model_Entity_Attribute
) to the catalog one (Mage_Catalog_Model_Resource_Eav_Attribute
).
因为从 1.4.x Magento 有单独的目录和客户模型属性模型,catalog_product
现在默认源模型的定义从 EAV 属性模型 ( Mage_Eav_Model_Entity_Attribute
) 移到目录一 ( Mage_Catalog_Model_Resource_Eav_Attribute
)。
As a result, some catalog attributes won't work with the EAV attribute model. Particularly those that use Mage_Eav_Model_Entity_Attribute_Source_Table
but don't explicitly define it (color, manufacturer, etc.).
因此,某些目录属性不适用于 EAV 属性模型。特别是那些使用Mage_Eav_Model_Entity_Attribute_Source_Table
但没有明确定义它的(颜色、制造商等)。
The following code snippet should work perfectly on your installation:
以下代码片段应该在您的安装中完美运行:
$attribute = Mage::getSingleton('eav/config')
->getAttribute(Mage_Catalog_Model_Product::ENTITY, 'color');
if ($attribute->usesSource()) {
$options = $attribute->getSource()->getAllOptions(false);
}
By the way Mage_Eav_Model_Config
model has a lot of helpful methods, that can be used in your development, so don't hesitate to look into this model.
顺便说一下,Mage_Eav_Model_Config
模型有很多有用的方法,可以在您的开发中使用,所以不要犹豫,看看这个模型。
回答by Tuong Le
The above code does not work if the resource_model is empty. The following snippet does the job:
如果 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);
回答by amit vyas
$attribute = Mage::getModel('eav/config')->getAttribute('customer','cateinterest');
$options = $attribute->getSource()->getAllOptions();
回答by Mohammed Muzammil
<?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
回答by Joseph Mastey
Sorry for an incomplete answer, but take a look at the database, specifically in the backend_model
column. I seem to remember having this same problem until I set this field to match some of the system fields in this respect.
抱歉,答案不完整,但请查看数据库,特别是在backend_model
列中。我似乎记得有同样的问题,直到我将此字段设置为在这方面匹配某些系统字段。