Jump to content

[SOLVED] null fields in DB


Recommended Posts

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

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

  • 2 years later...

My solution for PS 1.6.1.x (after two years, I know, but just to keep a note on a rare question):

  1. check that the field is nullable in the DB
  2. 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],
               ...
            ],
        ];
    
  3. 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 by __zac__ (see edit history)
Link to comment
Share on other sites

  • 4 months later...

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 by Marius05 (see edit history)
Link to comment
Share on other sites

×
×
  • Create New...