This is the well-known (annoying) behavior of Magento Adminhtml forms.
The problem is that if a value is not selected for the multi selector, the value for this attribute will not be sent when the form is submitted.
On the server side, Magento downloads the model, sets all published attribute values to the model, and saves it.
Since no value was published, the original value loaded into the model was not updated.
As a solution for attributes with a custom source model, I want to provide an empty option with a special parameter value (for example, -1). This value should not be 0or an empty string.
- , _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;
}
}
:
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);
}
}
, .