Magento 以编程方式创建属性选项 - 转义
Magento programmatically creating attribute options - escaping
我正在使用脚本以编程方式创建属性选项 - 代码如下:
echo "Creating option ....\n";
$attr_model = Mage::getModel('catalog/resource_eav_attribute');
$attr = $attr_model->loadByCode('catalog_product', $key);
$attr_id = $attr->getAttributeId();
if (!$attr_id){
echo "Cannot find Attribute $code\n";
return 0;
}else{
$option['attribute_id'] = $attr_id;
$option['value'][$value][0] = $value;
$option['value'][$value][1] = $value;
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttributeOption($option);
}
对于大多数值,它工作得非常好。
然而,某些值没有被创建 - 它没有抛出任何错误,但它没有创建选项。
一个例子是 55 mg/kg,这让我怀疑这是一个转义问题。
我需要为此遵循任何特定的转义规则吗?
谢谢!
深入研究后,我发现如果您在 $option['value'] 数组中使用的数组键以数字开头,Magento 会将其视为 addAttributeOption( ) 调用(将其转换为 (int),然后假设您正在编辑现有的属性选项)
确保数组键不以数字开头(我通过在键前添加 'opt' 来解决这个问题。
我正在使用脚本以编程方式创建属性选项 - 代码如下:
echo "Creating option ....\n";
$attr_model = Mage::getModel('catalog/resource_eav_attribute');
$attr = $attr_model->loadByCode('catalog_product', $key);
$attr_id = $attr->getAttributeId();
if (!$attr_id){
echo "Cannot find Attribute $code\n";
return 0;
}else{
$option['attribute_id'] = $attr_id;
$option['value'][$value][0] = $value;
$option['value'][$value][1] = $value;
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttributeOption($option);
}
对于大多数值,它工作得非常好。
然而,某些值没有被创建 - 它没有抛出任何错误,但它没有创建选项。
一个例子是 55 mg/kg,这让我怀疑这是一个转义问题。
我需要为此遵循任何特定的转义规则吗?
谢谢!
深入研究后,我发现如果您在 $option['value'] 数组中使用的数组键以数字开头,Magento 会将其视为 addAttributeOption( ) 调用(将其转换为 (int),然后假设您正在编辑现有的属性选项)
确保数组键不以数字开头(我通过在键前添加 'opt' 来解决这个问题。