Skip to content

Commit fa83011

Browse files
Merge pull request #8353 from Sesquipedalian/3.0/insert_ignore_returning
Fixes a bug when trying to return an ID from an INSERT IGNORE in MySQL
2 parents d44d12d + 8059c1d commit fa83011

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

Sources/Db/APIs/MySQL.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -468,30 +468,30 @@ public function insert(string $method, string $table, array $columns, array $dat
468468
}
469469
// the inserted value already exists we need to find the pk
470470
else {
471-
$where_string = '';
471+
$where_string = [];
472472

473-
$count2 = count($keys);
474-
475-
for ($x = 0; $x < $count2; $x++) {
476-
$keyPos = array_search($keys[$x], array_keys($columns));
477-
478-
$where_string .= $keys[$x] . ' = ' . $data[$i][$keyPos];
479-
480-
if (($x + 1) < $count2) {
481-
$where_string .= ' AND ';
473+
foreach ($columns as $column_name => $type) {
474+
if (str_contains($type, 'string-')) {
475+
$where_string[] = $column_name . ' = ' . sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . ')', $column_name);
476+
} else {
477+
$where_string[] = $column_name . ' = ' . sprintf('{%1$s:%2$s}', $type, $column_name);
482478
}
483479
}
484480

481+
$where_string = implode(' AND ', $where_string);
482+
485483
$request = $this->query(
486484
'',
487-
'SELECT `' . $keys[0] . '` FROM ' . $table . '
488-
WHERE ' . $where_string . ' LIMIT 1',
489-
[],
485+
'SELECT ' . $keys[0] . '
486+
FROM ' . $table . '
487+
WHERE ' . $where_string . '
488+
LIMIT 1',
489+
array_combine($indexed_columns, $data[$i]),
490490
);
491491

492492
if ($request !== false && $this->num_rows($request) == 1) {
493493
$row = $this->fetch_assoc($request);
494-
$ai = $row[$keys[0]];
494+
$ai = (int) $row[$keys[0]];
495495
}
496496
}
497497

0 commit comments

Comments
 (0)