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 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/Driver/Oracle/EventsTraitTest.php b/tests/Driver/Oracle/EventsTraitTest.php index 5f8b936aa..fa93ed69c 100644 --- a/tests/Driver/Oracle/EventsTraitTest.php +++ b/tests/Driver/Oracle/EventsTraitTest.php @@ -9,6 +9,11 @@ final class EventsTraitTest extends \Yiisoft\ActiveRecord\Tests\EventsTraitTest { + public function testSetValueOnUpdateOnUpsertWithUpdatePropertiesFalse(): void + { + $this->markTestSkipped('Yiisoft\Db\Oracle\DMLQueryBuilder::upsertReturning() is not supported by Oracle.'); + } + protected static function createConnection(): ConnectionInterface { return (new OracleHelper())->createConnection(); diff --git a/tests/EventsTraitTest.php b/tests/EventsTraitTest.php index f3fbf309b..af7292878 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 testSetValueOnUpdateOnUpsertWithUpdatePropertiesFalse(): void + { + $model = new SetValueOnUpdateAr(); + $model->id = 1; + $model->name = 'Vasya'; + + $model->upsert(['id' => 1], false); + + $this->assertSame('Vasya', $model->name); + } }