-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Closed
Labels
Description
Steps to reproduce the issue
reproduce:
insert a row where DateTime column is '0' -> in this case insertion does not fail and a new row is inserted
update a row where DateTime column is '0' -> an error is provided from mysql (Incorrect datetime value: '0' for column... )
Expected result
- either both should fail
- or none should fail
there seems to be a code inconsitancy in Joomla 4 database drivers as insertObject ignores some of the columns with code:
\libraries\vendor\joomla\database\src\Mysql\MysqlDriver.php
foreach (get_object_vars($object) as $k => $v)
{
// Skip columns that don't exist in the table.
if (!array_key_exists($k, $tableColumns))
{
continue;
}
// Only process non-null scalars.
if (\is_array($v) || \is_object($v) || $v === null)
{
continue;
}
// Ignore any internal fields.
if ($k[0] === '_')
{
continue;
}
// Ignore null datetime fields.
if ($tableColumns[$k] === 'datetime' && empty($v))
{
continue;
}
// Ignore null integer fields.
if (stristr($tableColumns[$k], 'int') !== false && $v === '')
{
continue;
}
// Prepare and sanitize the fields and values for the database query.
$fields[] = $this->quoteName($k);
$values[] = $this->quote($v);
}
while updateObject does not use same logic as above:
\libraries\vendor\joomla\database\src\DatabaseDriver.php
foreach (get_object_vars($object) as $k => $v)
{
// Skip columns that don't exist in the table.
if (!\array_key_exists($k, $tableColumns))
{
continue;
}
// Only process scalars that are not internal fields.
if (\is_array($v) || \is_object($v) || $k[0] === '_')
{
continue;
}
// Set the primary key to the WHERE clause instead of a field to update.
if (\in_array($k, $key, true))
{
$where[] = $this->quoteName($k) . ($v === null ? ' IS NULL' : ' = ' . $this->quote($v));
continue;
}
// Prepare and sanitize the fields and values for the database query.
if ($v === null)
{
// If the value is null and we want to update nulls then set it.
if ($nulls)
{
$val = 'NULL';
}
else
{
// If the value is null and we do not want to update nulls then ignore this field.
continue;
}
}
else
{
// The field is not null so we prep it for update.
$val = $this->quote($v);
}
// Add the field to be updated.
$fields[] = $this->quoteName($k) . '=' . $val;
}
i believe both of them should use same definition on columns which are going to be ignored.
best regards, stan