adr*_*n54 5 admin magento multiple-select
我创建了一个带有一些值的新属性(多选),一切正常,但是当我想删除产品的所有选定值时,我收到消息"产品属性已保存".但仍然选择了这些值.
笔记:
Ctrl + Click保存之前按下取消选择最后一个值.
我的想法已经用完了,谢谢你的帮助.
Vin*_*nai 11
这是Magento Adminhtml表单的已知(烦人)行为.
问题是如果没有为多选项选择任何值,则在提交表单时不会发布该属性的值.
在服务器端,Magento然后加载模型,在模型上设置所有已发布的属性值并保存.
由于未发布任何值,因此未更新模型上加载的原始值.
作为具有自定义源模型的属性的解决方案,我倾向于提供具有特殊选项值的空选项(例如-1).该值不能是0空字符串.
然后我为该属性指定一个后端模型,用于检查方法中的特殊值_beforeSave().如果找到后端模型,则取消设置模型实例上的属性.
这是一个例子:
来源模型:
class Your_Module_Model_Entity_Attribute_Source_Example
extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
{
const EMPTY = '-1';
public function getAllOptions()
$options = array(
array('value' => 1, 'label' => 'One'),
array('value' => 2, 'label' => 'Two'),
array('value' => 3, 'label' => 'Three')
);
if ($this->getAttribute()->getFrontendInput() === 'multiselect')
{
array_unshift($options, array('value' => self::EMPTY, 'label' => ''));
}
return $options;
}
}
Run Code Online (Sandbox Code Playgroud)
后端型号:
class Your_Module_Model_Entity_Attribute_Backend_Example
extends Mage_Eav_Model_Entity_Attribute_Backend_Abstract
{
public function beforeSave($object)
{
$code = $this->getAttribute()->getAttributeCode();
$value = $object->getData($code);
if ($value == Your_Module_Model_Entity_Attribute_Source_Example::EMPTY)
{
$object->unsetData($code);
}
return parent::beforeSave($object);
}
}
Run Code Online (Sandbox Code Playgroud)
如果您找到更好的解决方法,请告诉我.