我通过脚本创建Magento属性选项,但我需要能够获取新ID并立即在同一个脚本中使用它。
目前它还没有通过id - 如果我杀死脚本并重新启动它,它会选择创建的选项并返回ID,但不会作为同一脚本的一部分。
以下是我正在使用的代码:
$attr = Mage::getModel('catalog/product')->getResource()->getAttribute($key);
if ($attr->usesSource()) {
$vattr_id = $attr->getSource()->getOptionId($value);
}else{
echo "No Source";
$vattr_id = false;
}
if($vattr_id){
return $vattr_id;
}else{
$attr_model = Mage::getModel('catalog/resource_eav_attribute');
$attr = $attr_model->loadByCode('catalog_product', $key);
$attr_id = $attr->getAttributeId();
$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);
$attr = Mage::getModel('catalog/product')->getResource()->getAttribute($key);
if ($attr->usesSource()) {
$vattr_id = $attr->getSource()->getOptionId($value);
echo "AttrID: $vattr_id";
}
}
运行此项(使用所需的Mage::app()
等)会创建该选项,您可以在Magento后端看到它,但$vattr_id
是NULL
。如果我重新加载脚本,那么它会在第一个块中找到属性选项。
我想这与Magento如何缓存模型有关,但不确定我需要在哪些地方清除它们?