Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Bug #558: Fix `SoftDelete` with initiated custom date (@Tigrov)
- Enh #564: Clarify `$relations` parameter type in `JoinWith::__construct()` from `array<string|Closure>` to
`array<string|callable(ActiveQueryInterface):void>` (@vjik)
- Bug #561: Fix `ActiveRecordInterface::upsert()` with `$updateProperties = false` (@Tigrov)

## 1.0.2 March 11, 2026

Expand Down
14 changes: 8 additions & 6 deletions src/Event/Handler/SetValueOnUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
5 changes: 5 additions & 0 deletions tests/Driver/Oracle/EventsTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
12 changes: 12 additions & 0 deletions tests/EventsTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
Loading