Skip to content

Commit 3d97a9b

Browse files
authored
Merge pull request #1028 from cakephp/fix/adapter-empty-not-null-conflict-columns
Fix spurious warning when using empty conflictColumns with MySQL
2 parents debd054 + 74a30e9 commit 3d97a9b

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/Db/Adapter/AbstractAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ protected function getUpsertClause(?InsertMode $mode, ?array $updateColumns, ?ar
745745
return '';
746746
}
747747

748-
if ($conflictColumns !== null) {
748+
if ($conflictColumns !== null && $conflictColumns !== []) {
749749
trigger_error(
750750
'The $conflictColumns parameter is ignored by MySQL. ' .
751751
'MySQL\'s ON DUPLICATE KEY UPDATE applies to all unique constraints on the table.',

tests/TestCase/Db/Adapter/MysqlAdapterTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3080,6 +3080,38 @@ public function testInsertOrUpdateModeResetsAfterSave()
30803080
])->save();
30813081
}
30823082

3083+
public function testInsertOrUpdateWithEmptyConflictColumnsDoesNotWarn()
3084+
{
3085+
$table = new Table('currencies', [], $this->adapter);
3086+
$table->addColumn('code', 'string', ['limit' => 3])
3087+
->addColumn('rate', 'decimal', ['precision' => 10, 'scale' => 4])
3088+
->addIndex('code', ['unique' => true])
3089+
->create();
3090+
3091+
$warning = null;
3092+
set_error_handler(function (int $errno, string $errstr) use (&$warning) {
3093+
$warning = $errstr;
3094+
3095+
return true;
3096+
}, E_USER_WARNING);
3097+
3098+
try {
3099+
$table->insertOrUpdate([
3100+
['code' => 'USD', 'rate' => 1.0000],
3101+
['code' => 'EUR', 'rate' => 0.9000],
3102+
], ['rate'], [])->save();
3103+
} finally {
3104+
restore_error_handler();
3105+
}
3106+
3107+
$this->assertNull($warning, 'Empty conflictColumns should not trigger a warning for MySQL');
3108+
3109+
$rows = $this->adapter->fetchAll('SELECT * FROM currencies ORDER BY code');
3110+
$this->assertCount(2, $rows);
3111+
$this->assertEquals('0.9000', $rows[0]['rate']);
3112+
$this->assertEquals('1.0000', $rows[1]['rate']);
3113+
}
3114+
30833115
public function testCreateTableWithRangeColumnsPartitioning()
30843116
{
30853117
// MySQL requires RANGE COLUMNS for DATE columns

0 commit comments

Comments
 (0)