I am having an issue getting a custom model's value to update to NULL.

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

This code does not update the field custom_value in the database, and the database value accepts NULL values (Manually setting NULL in the database works fine).

EDIT: As Tim mentions below using Zend_Db_Expr("NULL") does set the value to null, could anyone explain why this is required in Magento and not just simply a php NULL ?

  • just try $model->setCustomValue(""); – Mufaddal Jul 2 '14 at 11:10
  • The type of the table field is double and I have tried other numeric types like decimal and doing $model->setCustomValue(""); sets the value to 0 – Invoke Jul 2 '14 at 11:36
  • 7
    How about $null = new Zend_Db_Expr("NULL"); ? – user487772 Jul 2 '14 at 11:42
  • 2
    @Invoke, to reply to your answer, if you only set it to "NULL" Magento will only think you don't want to update the value. As Tim suggested, you need to specify that it's the database "NULL" that you want to set. – Mayers Jul 2 '14 at 14:49
  • Thanks @mayers-dev, interesting behaviour. I guess it was designed as a get-out-clause should you need to cancel the intended update while generating the value itself. – Invoke Jul 2 '14 at 15:24

This is, as so often a isset() vs array_key_exists() problem, when I see this correctly.

I didn't try it, but from code reading I would say, the value is checked here:

if (!isset($this->_attributeValuesToSave[$table])) {
    $this->_attributeValuesToSave[$table] = array();

and because isset return false on a set array key which is NULL, your value is overwritten.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.