Rubens Cury Posted April 8, 2015 Share Posted April 8, 2015 (edited) 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 April 8, 2015 by Rubens Cury (see edit history) Link to comment Share on other sites More sharing options...
Rubens Cury Posted April 8, 2015 Author Share Posted April 8, 2015 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 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now