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
21 changes: 9 additions & 12 deletions src/AbstractActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,8 @@ abstract protected function propertyValuesInternal(): array;
* @throws InvalidArgumentException
* @throws InvalidConfigException
* @throws Throwable
*
* @return bool Whether the record inserted successfully.
*/
abstract protected function insertInternal(array|null $properties = null): bool;
abstract protected function insertInternal(array|null $properties = null): void;

/**
* Sets the value of the named property.
Expand All @@ -103,7 +101,7 @@ abstract protected function populateProperty(string $name, mixed $value): void;
abstract protected function upsertInternal(
array|null $insertProperties = null,
array|bool $updateProperties = true,
): bool;
): void;

public function createQuery(ActiveRecordInterface|string|null $modelClass = null): ActiveQueryInterface
{
Expand Down Expand Up @@ -370,9 +368,9 @@ public function hasOne(ActiveRecordInterface|string $modelClass, array $link): A
return $this->createRelationQuery($modelClass, $link, false);
}

public function insert(array|null $properties = null): bool
public function insert(array|null $properties = null): void
{
return $this->insertInternal($properties);
$this->insertInternal($properties);
}

public function isChanged(): bool
Expand Down Expand Up @@ -608,15 +606,14 @@ protected function retrieveRelation(string $name): ActiveRecordInterface|array|n
return $this->related[$name] = $query->relatedRecords();
}

public function save(array|null $properties = null): bool
public function save(array|null $properties = null): void
{
if ($this->isNewRecord()) {
return $this->insert($properties);
$this->insert($properties);
return;
}

$this->update($properties);

return true;
}

public function set(string $propertyName, mixed $value): void
Expand Down Expand Up @@ -794,9 +791,9 @@ public function updateCounters(array $counters): bool
return true;
}

public function upsert(array|null $insertProperties = null, array|bool $updateProperties = true): bool
public function upsert(array|null $insertProperties = null, array|bool $updateProperties = true): void
{
return $this->upsertInternal($insertProperties, $updateProperties);
$this->upsertInternal($insertProperties, $updateProperties);
}

public function unlink(string $relationName, ActiveRecordInterface $linkedModel, bool $delete = false): void
Expand Down
18 changes: 6 additions & 12 deletions src/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ protected function propertyValuesInternal(): array
return get_object_vars($this);
}

protected function insertInternal(array|null $properties = null): bool
protected function insertInternal(array|null $properties = null): void
{
if (!$this->isNewRecord()) {
throw new InvalidCallException('The record is not new and cannot be inserted.');
Expand All @@ -168,10 +168,10 @@ protected function insertInternal(array|null $properties = null): bool
$values = $this->newPropertyValues($properties);
$primaryKeys = $this->db()->createCommand()->insertReturningPks($this->tableName(), $values);

return $this->populateRawValues($primaryKeys, $values);
$this->populateRawValues($primaryKeys, $values);
}

protected function upsertInternal(array|null $insertProperties = null, array|bool $updateProperties = true): bool
protected function upsertInternal(array|null $insertProperties = null, array|bool $updateProperties = true): void
{
if (!$this->isNewRecord()) {
throw new InvalidCallException('The record is not new and cannot be inserted.');
Expand Down Expand Up @@ -205,7 +205,7 @@ protected function upsertInternal(array|null $insertProperties = null, array|boo
$returnedValues = $this->db()->createCommand()
->upsertReturning($this->tableName(), $insertValues, $updateProperties, $returnProperties);

return $this->populateRawValues($returnedValues);
$this->populateRawValues($returnedValues);
}

protected function populateProperty(string $name, mixed $value): void
Expand All @@ -214,23 +214,17 @@ protected function populateProperty(string $name, mixed $value): void
}

/**
* @psalm-param array<string, mixed>|false $rawValues
* @psalm-param array<string, mixed> $rawValues
*/
private function populateRawValues(array|false $rawValues, array $oldValues = []): bool
private function populateRawValues(array $rawValues, array $oldValues = []): void
{
if ($rawValues === false) {
return false;
}

$values = $this->phpTypecastValues($rawValues);

foreach ($values as $name => $value) {
$this->set($name, $value);
}

$this->assignOldValues(array_merge($oldValues, $values));

return true;
}

/**
Expand Down
12 changes: 3 additions & 9 deletions src/ActiveRecordInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,8 @@ public function hasProperty(string $name): bool;
* @throws InvalidCallException If the record {@see isNewRecord() is not new}.
* @throws InvalidConfigException
* @throws Throwable In case insert failed.
*
* @return bool Whether the record is inserted successfully.
*/
public function insert(array|null $properties = null): bool;
public function insert(array|null $properties = null): void;

/**
* Checks if any property returned by {@see propertyNames()} method has changed.
Expand Down Expand Up @@ -424,10 +422,8 @@ public function resetRelation(string $name): void;
*
* @param array|null $properties List of property names or name-values pairs that need to be saved.
* Defaults to `null`, meaning all changed property values will be saved.
*
* @return bool Whether the saving succeeded (that's no validation errors occurred).
*/
public function save(array|null $properties = null): bool;
public function save(array|null $properties = null): void;

/**
* Sets the named property value.
Expand Down Expand Up @@ -562,10 +558,8 @@ public function updateAll(array $propertyValues, array|string $condition = [], a
*
* @throws InvalidConfigException
* @throws Throwable In case query failed.
*
* @return bool Whether the record is inserted or updated successfully.
*/
public function upsert(array|null $insertProperties = null, array|bool $updateProperties = true): bool;
public function upsert(array|null $insertProperties = null, array|bool $updateProperties = true): void;

/**
* Destroys the relationship between two records.
Expand Down
3 changes: 1 addition & 2 deletions src/Event/AfterInsert.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ final class AfterInsert extends AbstractEvent
{
/**
* @param ActiveRecordInterface $model The model that has been inserted.
* @param bool $isSuccessful Whether the insert operation is successful.
*/
public function __construct(ActiveRecordInterface $model, public bool &$isSuccessful)
public function __construct(ActiveRecordInterface $model)
{
parent::__construct($model);
}
Expand Down
3 changes: 1 addition & 2 deletions src/Event/AfterSave.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ final class AfterSave extends AbstractEvent
{
/**
* @param ActiveRecordInterface $model The model that was saved.
* @param bool $isSuccessful Whether the save operation was successful.
*/
public function __construct(ActiveRecordInterface $model, public bool &$isSuccessful)
public function __construct(ActiveRecordInterface $model)
{
parent::__construct($model);
}
Expand Down
3 changes: 1 addition & 2 deletions src/Event/AfterUpsert.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ final class AfterUpsert extends AbstractEvent
{
/**
* @param ActiveRecordInterface $model The model that was upserted.
* @param bool $isSuccessful Whether the upsert operation was successful.
*/
public function __construct(ActiveRecordInterface $model, public bool &$isSuccessful)
public function __construct(ActiveRecordInterface $model)
{
parent::__construct($model);
}
Expand Down
30 changes: 12 additions & 18 deletions src/Trait/EventsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,18 @@ public function delete(): int
return $result;
}

public function insert(array|null $properties = null): bool
public function insert(array|null $properties = null): void
{
$eventDispatcher = EventDispatcherProvider::get(static::class);
$eventDispatcher->dispatch($event = new BeforeInsert($this, $properties));

if ($event->isDefaultPrevented()) {
return $event->getReturnValue() ?? false;
return;
}

$result = parent::insert($properties);
parent::insert($properties);

$eventDispatcher->dispatch(new AfterInsert($this, $result));

return $result;
$eventDispatcher->dispatch(new AfterInsert($this));
}

public function populateRecord(array|object $data): static
Expand Down Expand Up @@ -109,20 +107,18 @@ public static function query(ActiveRecordInterface|Closure|null|string $modelCla
return $query;
}

public function save(array|null $properties = null): bool
public function save(array|null $properties = null): void
{
$eventDispatcher = EventDispatcherProvider::get(static::class);
$eventDispatcher->dispatch($event = new BeforeSave($this, $properties));

if ($event->isDefaultPrevented()) {
return $event->getReturnValue() ?? false;
return;
}

$result = parent::save($properties);

$eventDispatcher->dispatch(new AfterSave($this, $result));
parent::save($properties);

return $result;
$eventDispatcher->dispatch(new AfterSave($this));
}

public function update(array|null $properties = null): int
Expand All @@ -141,19 +137,17 @@ public function update(array|null $properties = null): int
return $result;
}

public function upsert(array|null $insertProperties = null, array|bool $updateProperties = true): bool
public function upsert(array|null $insertProperties = null, array|bool $updateProperties = true): void
{
$eventDispatcher = EventDispatcherProvider::get(static::class);
$eventDispatcher->dispatch($event = new BeforeUpsert($this, $insertProperties, $updateProperties));

if ($event->isDefaultPrevented()) {
return $event->getReturnValue() ?? false;
return;
}

$result = parent::upsert($insertProperties, $updateProperties);

$eventDispatcher->dispatch(new AfterUpsert($this, $result));
parent::upsert($insertProperties, $updateProperties);

return $result;
$eventDispatcher->dispatch(new AfterUpsert($this));
}
}
4 changes: 2 additions & 2 deletions tests/ActiveQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2065,12 +2065,12 @@ public function testOldPropertyAfterInsertAndUpdate(): void
]);

$this->assertNull($customer->oldValue('name'));
$this->assertTrue($customer->save());
$customer->save();
$this->assertSame('Jack', $customer->oldValue('name'));

$customer->set('name', 'Harry');

$this->assertTrue($customer->save());
$customer->save();
$this->assertSame('Harry', $customer->oldValue('name'));
}

Expand Down
Loading
Loading