Jump to content

Replacing NULL by ZERO


Rubens Cury

Recommended Posts

Hi,
 
I've noticed when I add or update any record of my custom tables module, Prestashop Core is replacing all fields defined as NULL by ZERO, including datetime fields.
 
The Db.php update function

	/**
	 * @param string $table Table name without prefix
	 * @param array $data Data to insert as associative array. If $data is a list of arrays, multiple insert will be done
	 * @param string $where WHERE condition
	 * @param int $limit
	 * @param bool $null_values If we want to use NULL values instead of empty quotes
	 * @param bool $use_cache
	 * @param bool $add_prefix Add or not _DB_PREFIX_ before table name
	 * @return bool
	 */
	public function update($table, $data, $where = '', $limit = 0, $null_values = false, $use_cache = true, $add_prefix = true)
	{
		if (!$data)
			return true;

		if ($add_prefix)
			$table = _DB_PREFIX_.$table;

		$sql = 'UPDATE `'.bqSQL($table).'` SET ';
		foreach ($data as $key => $value) {
            if (!is_array($value))
                $value = array('type' => 'text', 'value' => $value);
            if ($value['type'] == 'sql')
                $sql .= '`' . bqSQL($key) . "` = {$value['value']},";
            else
                $sql .= ($null_values && ($value['value'] === '' || is_null($value['value']))) ? '`' . bqSQL($key) . '` = NULL,' : '`' . bqSQL($key) . "` = '{$value['value']}',";
		}



		$sql = rtrim($sql, ',');
		if ($where)
			$sql .= ' WHERE '.$where;
		if ($limit)
			$sql .= ' LIMIT '.(int)$limit;
		return (bool)$this->q($sql, $use_cache);
	}

The part of the code executed when I call the update method through my object class by $myclass->update(true,true) is

($null_values && ($value['value'] === '' || is_null($value['value']))) ? '`' . bqSQL($key) . '` = NULL,' : '`' . bqSQL($key) . "` = '{$value['value']}',";

The problem is when this line is executed, all the NULL $values were already replaced by ZERO.

 

Any help?

 

Thanks

Edited by Rubens Cury (see edit history)
Link to comment
Share on other sites

Just found the solution.

 

All values are formatted by ObjectModel::formatValue() according the TYPES declared in your definition fields class.

 

All we have to do is to set TYPE_NOTHING for each field in which we DON'T want to replace NULL by the default format type.

 

Hope it helps,

Rubens Cury

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...