topall Posted November 14, 2013 Share Posted November 14, 2013 Hello. I have a very simple piece of code. $dropOrder = new DropOrder($dropOrderId); $dropOrder->is_supplier_paid = $payValue; $dropOrder->save(); It works and saves a 'is_supplier_paid' field value into DB. But it also does unexpected actions and fills all null valued fields with 0 values. I try to save it like this $dropOrder->save(true); But I still have the same strange behavior. I want to change one field only and don't touch the other ones. Does anybody know any clues to solve my problem? Thank you. Link to comment Share on other sites More sharing options...
topall Posted November 18, 2013 Author Share Posted November 18, 2013 Solution from stackoverflow.com Values are formated by ObjectModel::formatValue() before they are inserted / updated, based on the type of the field declared in your $definition. You have to use TYPE_NOTHING to allow NULL values, it's the only way. Take a look in Configuration class, with id_shop and id_group_shop fields. Link to comment Share on other sites More sharing options...
vekia Posted November 18, 2013 Share Posted November 18, 2013 thank you for solution, i marked this topic as [solved] with regards, Milos Link to comment Share on other sites More sharing options...
__zac__ Posted April 22, 2016 Share Posted April 22, 2016 (edited) My solution for PS 1.6.1.x (after two years, I know, but just to keep a note on a rare question): check that the field is nullable in the DB your objectmodel should provide the following (allow_null is important): public static $definition = [ ... 'fields' => [ ... 'YOUR_NULLABLE_FIELD' => ['type' => self::TYPE_INT, 'validate' => 'isNullOrUnsignedId', 'allow_null' =>true], ... ], ]; override the update() method of your objectmodel to set null value when the field is 0 public function update($null_values = false) { if ($this->YOUR_NULLABLE_FIELD == 0) { $this->parent_id = null; } return parent::update($null_values); } Doing this way the base class will not reformat the null value to 0 Edited April 22, 2016 by __zac__ (see edit history) Link to comment Share on other sites More sharing options...
Marius05 Posted September 22, 2016 Share Posted September 22, 2016 (edited) Hi! I have this class in my custom module class DefaultAttributeUpdateoObject extends ObjectModel { public $id_produs_comandat; public $id_product_attribute; /** * @see ObjectModel::$definition */ public static $definition = array( 'table' => 'product_attribute', 'primary' => 'id_product_attribute', 'multilang' => false, 'fields' => array( 'default_on' => array('type' => self::TYPE_NOTHING, 'validate' => 'isUnsignedId', 'allow_null' =>true) ) ); public function update($null_values = false) { if ($this->default_on == 0) { $this->parent_id = null; } return parent::update($null_values); } } In custommodule.php file i want to update column default_on from table ps_product_attribute with NULL value. How ken i do that ? Than you! Edited September 22, 2016 by Marius05 (see edit history) Link to comment Share on other sites More sharing options...
Recommended Posts