これは、Magento Adminhtmlフォームの既知の(厄介な)動作です。
問題は、マルチセレクションに値が選択されていない場合、フォームが送信されるときに、その属性の値がポストされないことです。
サーバー側では、モデルを読み込み、モデルに投稿されたすべての属性値を設定して保存します。
値がポストされていないため、モデルにロードされた元の値は更新されませんでした。
カスタムソースモデルの属性の解決策として、空のオプションに特別なオプション値(例:-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;
}
}
バックエンドモデル:
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);
}
}
あなたがより良い回避策を見つけた場合は私に知らせてください。
あなたのコードを共有していただきありがとうございます。 – adrien54