在Magento模型上将值更新为NULL的正确方法是什么?


12

我在获取自定义模型的值以更新为NULL时遇到问题。

$model = Mage::getModel('custom/model')->load($id);
$model->setCustomValue(NULL);
$model->save();

此代码不会更新custom_value数据库中的字段,并且数据库值接受NULL值(在数据库中手动设置NULL可以正常工作)。

编辑: 正如Tim在下面提到的using Zend_Db_Expr("NULL")会将值设置为null,有人可以解释为什么在Magento中需要这样做,而不仅仅是一个php NULL吗?


只需尝试$ model-> setCustomValue(“”);
Mufaddal 2014年

表字段的类型为double,我尝试了其他数值类型,例如decimal$model->setCustomValue("");并将值设置为0
调用

7
怎么$null = new Zend_Db_Expr("NULL");
user487772 2014年

2
@Invoke,如果只将其设置为“ NULL”,则答复您的答案。Magento只会认为您不想更新该值。正如Tim所建议的,您需要指定它是您要设置的数据库“ NULL”。
Mayers 2014年

感谢@ mayers-dev,有趣的行为。我想如果您需要在生成值本身时取消预期的更新,则它被设计为解决子句。
调用

Answers:


2

当我正确看到此问题时,这通常是一个isset()vs array_key_exists()问题。

我没有尝试过,但是从代码读取中我会说,这里检查了值:

\Mage_Eav_Model_Entity_Abstract::_saveAttribute
if (!isset($this->_attributeValuesToSave[$table])) {
    $this->_attributeValuesToSave[$table] = array();
}

并且由于isset在设置的数组键上返回false NULL,因此您的值将被覆盖。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.