From 829dfba093f33669838070017ed93e09a3f7319d Mon Sep 17 00:00:00 2001 From: Tigrov Date: Thu, 30 Apr 2026 15:06:19 +0700 Subject: [PATCH 1/4] Fix `upsert()` with `$updateProperties = false` --- src/Event/Handler/SetValueOnUpdate.php | 14 ++++++++------ tests/EventsTraitTest.php | 12 ++++++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/Event/Handler/SetValueOnUpdate.php b/src/Event/Handler/SetValueOnUpdate.php index e4c09772d..be698ff0a 100644 --- a/src/Event/Handler/SetValueOnUpdate.php +++ b/src/Event/Handler/SetValueOnUpdate.php @@ -49,19 +49,21 @@ private function beforeUpdate(BeforeUpdate $event): void private function beforeUpsert(BeforeUpsert $event): void { + if ($event->updateProperties === false) { + return; + } + $model = $event->model; $value = is_callable($this->value) ? ($this->value)($event) : $this->value; foreach ($this->getPropertyNames() as $propertyName) { if ($model->hasProperty($propertyName)) { - $updateProperties ??= match ($event->updateProperties) { - true => array_diff_key( + $updateProperties ??= $event->updateProperties === true + ? array_diff_key( $event->insertProperties ?? $model->newValues(), array_fill_keys($model->primaryKey(), null), - ), - false => [], - default => $event->updateProperties, - }; + ) + : $event->updateProperties; $updateProperties[$propertyName] = $value; } diff --git a/tests/EventsTraitTest.php b/tests/EventsTraitTest.php index f3fbf309b..b1227705f 100644 --- a/tests/EventsTraitTest.php +++ b/tests/EventsTraitTest.php @@ -12,6 +12,7 @@ use Yiisoft\ActiveRecord\Event\BeforeUpsert; use Yiisoft\ActiveRecord\Event\EventDispatcherProvider; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CategoryEventsModel; +use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\SetValueOnUpdateAr; use Yiisoft\Test\Support\EventDispatcher\SimpleEventDispatcher; abstract class EventsTraitTest extends TestCase @@ -251,4 +252,15 @@ static function (object $event): void { $this->assertNull($model->id); $this->assertSame('Custom Return Upsert', $model->name); } + + public function testSetValueOnUpdateBeforeUpsertAddsPropertyWhenUpdatesAreDisabled(): void + { + $model = new SetValueOnUpdateAr(); + $model->id = 1; + $model->name = 'Vasya'; + + $model->upsert(['id' => 1], false); + + $this->assertSame('Vasya', $model->name); + } } From 4aed32adebf44dd8f3f13326f139e524395c2560 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Thu, 30 Apr 2026 15:48:42 +0700 Subject: [PATCH 2/4] Fix --- tests/Driver/Oracle/EventsTraitTest.php | 5 +++++ tests/EventsTraitTest.php | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/Driver/Oracle/EventsTraitTest.php b/tests/Driver/Oracle/EventsTraitTest.php index 5f8b936aa..f5e066379 100644 --- a/tests/Driver/Oracle/EventsTraitTest.php +++ b/tests/Driver/Oracle/EventsTraitTest.php @@ -13,4 +13,9 @@ protected static function createConnection(): ConnectionInterface { return (new OracleHelper())->createConnection(); } + + public function testSetValueOnUpdateOnUpsertWithUpdatePropertiesFalse(): void + { + $this->markTestSkipped('Yiisoft\Db\Oracle\DMLQueryBuilder::upsertReturning() is not supported by Oracle.'); + } } diff --git a/tests/EventsTraitTest.php b/tests/EventsTraitTest.php index b1227705f..af7292878 100644 --- a/tests/EventsTraitTest.php +++ b/tests/EventsTraitTest.php @@ -253,7 +253,7 @@ static function (object $event): void { $this->assertSame('Custom Return Upsert', $model->name); } - public function testSetValueOnUpdateBeforeUpsertAddsPropertyWhenUpdatesAreDisabled(): void + public function testSetValueOnUpdateOnUpsertWithUpdatePropertiesFalse(): void { $model = new SetValueOnUpdateAr(); $model->id = 1; From 6fc543d37610edbe7557fa2d999d9980b5c683a6 Mon Sep 17 00:00:00 2001 From: Tigrov <8563175+Tigrov@users.noreply.github.com> Date: Sat, 2 May 2026 09:17:29 +0000 Subject: [PATCH 3/4] Apply PHP CS Fixer and Rector changes (CI) --- tests/Driver/Oracle/EventsTraitTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Driver/Oracle/EventsTraitTest.php b/tests/Driver/Oracle/EventsTraitTest.php index f5e066379..fa93ed69c 100644 --- a/tests/Driver/Oracle/EventsTraitTest.php +++ b/tests/Driver/Oracle/EventsTraitTest.php @@ -9,13 +9,13 @@ final class EventsTraitTest extends \Yiisoft\ActiveRecord\Tests\EventsTraitTest { - protected static function createConnection(): ConnectionInterface + public function testSetValueOnUpdateOnUpsertWithUpdatePropertiesFalse(): void { - return (new OracleHelper())->createConnection(); + $this->markTestSkipped('Yiisoft\Db\Oracle\DMLQueryBuilder::upsertReturning() is not supported by Oracle.'); } - public function testSetValueOnUpdateOnUpsertWithUpdatePropertiesFalse(): void + protected static function createConnection(): ConnectionInterface { - $this->markTestSkipped('Yiisoft\Db\Oracle\DMLQueryBuilder::upsertReturning() is not supported by Oracle.'); + return (new OracleHelper())->createConnection(); } } From 77e2e609f0466af8c166329e874301d6a16e4961 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Sat, 2 May 2026 19:41:55 +0700 Subject: [PATCH 4/4] Add line to CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0159ad092..697709beb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - Bug #558: Fix `SoftDelete` with initiated custom date (@Tigrov) - Enh #564: Clarify `$relations` parameter type in `JoinWith::__construct()` from `array` to `array` (@vjik) +- Bug #561: Fix `ActiveRecordInterface::upsert()` with `$updateProperties = false` (@Tigrov) ## 1.0.2 March 11, 2026