Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix extensibility issue #278

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add BaseActiveRecord::setAttributeInternal() method
  • Loading branch information
Tigrov committed Dec 16, 2023
commit b2219f7eec647137680186235a1c69998d246cb5
23 changes: 16 additions & 7 deletions src/BaseActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -658,13 +658,7 @@
public function setAttribute(string $name, mixed $value): void
{
if ($this->hasAttribute($name)) {
if (
!empty($this->relationsDependencies[$name])
&& (!array_key_exists($name, $this->attributes) || $this->attributes[$name] !== $value)
) {
$this->resetDependentRelations($name);
}
$this->attributes[$name] = $value;
$this->setAttributeInternal($name, $value);
} else {
throw new InvalidArgumentException(static::class . ' has no attribute named "' . $name . '".');
}
Expand Down Expand Up @@ -1281,4 +1275,19 @@
}
return $data;
}

/**
* Sets the named attribute value without checking if the attribute exists.
*/
protected function setAttributeInternal(string $name, mixed $value): void
{
if (
!empty($this->relationsDependencies[$name])
&& ($value === null || $value !== $this->attributes[$name] ?? null)

Check failure on line 1286 in src/BaseActiveRecord.php

View workflow job for this annotation

GitHub Actions / PHP 8.1-ubuntu-latest

InvalidArgument: Isset only works with variables and array elements

Check failure on line 1286 in src/BaseActiveRecord.php

View workflow job for this annotation

GitHub Actions / PHP 8.1-ubuntu-latest

InvalidArgument: Isset only works with variables and array elements
) {
$this->resetDependentRelations($name);
}

$this->attributes[$name] = $value;
}
}
14 changes: 3 additions & 11 deletions src/BaseActiveRecordTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,7 @@
public function __unset(string $name): void
{
if ($this->hasAttribute($name)) {
unset($this->attributes[$name]);
if (!empty($this->relationsDependencies[$name])) {
$this->resetDependentRelations($name);
}
$this->setAttributeInternal($name, null);
} elseif (array_key_exists($name, $this->related)) {
unset($this->related[$name]);
}
Expand All @@ -189,13 +186,8 @@
public function __set(string $name, mixed $value): void
{
if ($this->hasAttribute($name)) {
if (
!empty($this->relationsDependencies[$name])
&& (!array_key_exists($name, $this->attributes) || $this->attributes[$name] !== $value)
) {
$this->resetDependentRelations($name);
}
$this->attributes[$name] = $value;
$this->setAttributeInternal($name, $value);
return;
}

if (method_exists($this, 'get' . ucfirst($name))) {
Expand Down Expand Up @@ -248,7 +240,7 @@
* @param mixed $offset the offset to set element.
* @param mixed $item the element value.
*/
public function offsetSet(mixed $offset, mixed $item): void

Check failure on line 243 in src/BaseActiveRecordTrait.php

View workflow job for this annotation

GitHub Actions / PHP 8.1-ubuntu-latest

ParamNameMismatch: Argument 2 of Yiisoft\ActiveRecord\BaseActiveRecordTrait::offsetSet has wrong name $item, expecting $value as defined by ArrayAccess::offsetSet

Check failure on line 243 in src/BaseActiveRecordTrait.php

View workflow job for this annotation

GitHub Actions / PHP 8.1-ubuntu-latest

ParamNameMismatch: Argument 2 of Yiisoft\ActiveRecord\BaseActiveRecordTrait::offsetSet has wrong name $item, expecting $value as defined by ArrayAccess::offsetSet
{
$this->$offset = $item;
}
Expand Down
Loading