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 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
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 save(array $attributeNames = null): bool
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 @@ public function toArray(): array
}
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))
) {
$this->resetDependentRelations($name);
}

$this->attributes[$name] = $value;
}
}
20 changes: 4 additions & 16 deletions src/BaseActiveRecordTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,8 @@
*/
public function __get(string $name)
{
if (isset($this->attributes[$name]) || array_key_exists($name, $this->attributes)) {
return $this->attributes[$name];
}

if ($this->hasAttribute($name)) {
return null;
return $this->getAttribute($name);
}

if (isset($this->related[$name]) || array_key_exists($name, $this->related)) {
Expand All @@ -76,11 +72,11 @@
return $this->$getter();
}

if (method_exists($this, 'set' . ucfirst($name))) {

Check warning on line 75 in src/BaseActiveRecordTrait.php

View workflow job for this annotation

GitHub Actions / PHP 8-ubuntu-latest

Escaped Mutant for Mutator "UnwrapUcFirst": --- Original +++ New @@ @@ /** read property, e.g. getName() */ return $this->{$getter}(); } - if (method_exists($this, 'set' . ucfirst($name))) { + if (method_exists($this, 'set' . $name)) { throw new InvalidCallException('Getting write-only property: ' . static::class . '::' . $name); } throw new UnknownPropertyException('Getting unknown property: ' . static::class . '::' . $name);
throw new InvalidCallException('Getting write-only property: ' . static::class . '::' . $name);
}

throw new UnknownPropertyException('Getting unknown property: ' . static::class . '::' . $name);

Check warning on line 79 in src/BaseActiveRecordTrait.php

View workflow job for this annotation

GitHub Actions / PHP 8-ubuntu-latest

Escaped Mutant for Mutator "ConcatOperandRemoval": --- Original +++ New @@ @@ if (method_exists($this, 'set' . ucfirst($name))) { throw new InvalidCallException('Getting write-only property: ' . static::class . '::' . $name); } - throw new UnknownPropertyException('Getting unknown property: ' . static::class . '::' . $name); + throw new UnknownPropertyException(static::class . '::' . $name); } /** * Returns the relation object with the specified name.
}

/**
Expand Down Expand Up @@ -171,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 @@ -193,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 @@ -252,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 Expand Up @@ -301,7 +289,7 @@

public function canGetProperty(string $name, bool $checkVars = true): bool
{
if (method_exists($this, 'get' . ucfirst($name)) || ($checkVars && property_exists($this, $name))) {

Check warning on line 292 in src/BaseActiveRecordTrait.php

View workflow job for this annotation

GitHub Actions / PHP 8-ubuntu-latest

Escaped Mutant for Mutator "UnwrapUcFirst": --- Original +++ New @@ @@ } public function canGetProperty(string $name, bool $checkVars = true) : bool { - if (method_exists($this, 'get' . ucfirst($name)) || $checkVars && property_exists($this, $name)) { + if (method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name)) { return true; } try {
return true;
}

Expand Down
11 changes: 11 additions & 0 deletions tests/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Cat;
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Customer;
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerClosureField;
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerExtraAttributes;
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerWithAlias;
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Dog;
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Item;
Expand Down Expand Up @@ -763,4 +764,14 @@ public function testToArrayWithClosure(): void
$customer->toArray(),
);
}

public function testGetExtraArrtibute()
{
$this->checkFixture($this->db, 'customer');

$customerQuery = new ActiveQuery(CustomerExtraAttributes::class, $this->db);
$customer = $customerQuery->findOne(1);

$this->assertSame($customer->sex, 'm');
}
}
48 changes: 48 additions & 0 deletions tests/Stubs/ActiveRecord/CustomerExtraAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord;

use Yiisoft\ActiveRecord\ActiveRecord;

/**
* Class CustomerClosureField.
*
* @property int $id
* @property string $name
* @property string $email
* @property string $address
* @property int $status
* @property string $sex
*/
final class CustomerExtraAttributes extends ActiveRecord
{
private array $extraAttributes = [
'sex' => 'm',
];

public function getTableName(): string
{
return 'customer';
}

public function getAttribute(string $name): mixed
{
if (array_key_exists($name, $this->extraAttributes)) {
return $this->extraAttributes[$name];
}

return parent::getAttribute($name);
}

public function getAttributes(array $names = null, array $except = []): array
{
return array_merge(parent::getAttributes($names, $except), $this->extraAttributes);
}

public function hasAttribute($name): bool
{
return array_key_exists($name, $this->extraAttributes) || parent::hasAttribute($name);
}
}
Loading