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

Refactor BaseActiveRecord::getDirtyAttributes() #283

Merged
merged 8 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
79 changes: 28 additions & 51 deletions src/BaseActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,22 @@
use Yiisoft\Db\Helper\DbStringHelper;

use function array_combine;
use function array_diff_key;
use function array_diff;
use function array_flip;
use function array_intersect;
use function array_intersect_key;
use function array_key_exists;
use function array_keys;
use function array_merge;
use function array_search;
use function array_values;
use function count;
use function get_object_vars;
use function in_array;
use function is_array;
use function is_int;
use function property_exists;
use function reset;

/**
Expand Down Expand Up @@ -135,21 +140,14 @@ public function getAttribute(string $name): mixed

public function getAttributes(array $names = null, array $except = []): array
{
$values = [];

if ($names === null) {
$names = $this->attributes();
}
$names ??= $this->attributes();
$attributes = array_merge($this->attributes, get_object_vars($this));

if ($except !== []) {
$names = array_diff($names, $except);
}

foreach ($names as $name) {
$values[$name] = $this->$name;
}

return $values;
return array_intersect_key($attributes, array_flip($names));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you bench it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about oldAttributes?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you bench it?

Before:
benchGetAttributes......................R1 I1 - Mo20.920μs (±1.00%)

After:
benchGetAttributes......................R2 I2 - Mo17.238μs (±1.78%)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about oldAttributes?

getOldAttributes() is a different method, without changes.

}

public function getIsNewRecord(): bool
Expand Down Expand Up @@ -185,41 +183,21 @@ public function getOldAttribute(string $name): mixed
*/
public function getDirtyAttributes(array $names = null): array
{
if ($names === null) {
$names = $this->attributes();
$attributes = $this->getAttributes($names);

if ($this->oldAttributes === null) {
return $attributes;
}

$names = array_flip($names);
$attributes = [];
$result = array_diff_key($attributes, $this->oldAttributes);

if ($this->oldAttributes === null) {
/**
* @var string $name
* @var mixed $value
*/
foreach ($this->attributes as $name => $value) {
if (isset($names[$name])) {
/** @psalm-var mixed */
$attributes[$name] = $value;
}
}
} else {
/**
* @var string $name
* @var mixed $value
*/
foreach ($this->attributes as $name => $value) {
if (
isset($names[$name])
&& (!array_key_exists($name, $this->oldAttributes) || $value !== $this->oldAttributes[$name])
) {
/** @psalm-var mixed */
$attributes[$name] = $value;
}
foreach (array_diff_key($attributes, $result) as $name => $value) {
if ($value !== $this->oldAttributes[$name]) {
$result[$name] = $value;
}
}

return $attributes;
return $result;
}

public function getOldAttributes(): array
Expand Down Expand Up @@ -584,21 +562,11 @@ public function optimisticLock(): string|null
*/
public function populateRecord(array|object $row): void
{
$columns = array_flip($this->attributes());

/**
* @psalm-var string $name
* @psalm-var mixed $value
*/
foreach ($row as $name => $value) {
if (isset($columns[$name])) {
$this->attributes[$name] = $value;
} elseif ($this->canSetProperty($name)) {
$this->$name = $value;
}
$this->populateAttribute($name, $value);
$this->oldAttributes[$name] = $value;
}

$this->oldAttributes = $this->attributes;
$this->related = [];
$this->relationsDependencies = [];
}
Expand Down Expand Up @@ -1266,4 +1234,13 @@ public function getTableName(): string

return $this->tableName;
}

private function populateAttribute(string $name, mixed $value): void
{
if (property_exists($this, $name)) {
$this->$name = $value;
} else {
$this->attributes[$name] = $value;
}
}
}
94 changes: 86 additions & 8 deletions tests/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerClosureField;
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerForArrayable;
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerWithAlias;
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerWithProperties;
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Dog;
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Item;
use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\NoExist;
Expand Down Expand Up @@ -597,14 +598,9 @@ public function testAttributeAccess(): void
$this->assertTrue($customer->canGetProperty('orderItems'));
$this->assertFalse($customer->canSetProperty('orderItems'));

try {
/** @var $itemClass ActiveRecordInterface */
$customer->orderItems = [new Item($this->db)];
$this->fail('setter call above MUST throw Exception');
} catch (Exception $e) {
/** catch exception "Setting read-only property" */
$this->assertInstanceOf(InvalidCallException::class, $e);
}
$this->expectException(InvalidCallException::class);
$this->expectExceptionMessage('Setting read-only property: ' . Customer::class . '::orderItems');
$customer->orderItems = [new Item($this->db)];

/** related attribute $customer->orderItems didn't change cause it's read-only */
$this->assertSame([], $customer->orderItems);
Expand Down Expand Up @@ -836,4 +832,86 @@ public function testToArrayForArrayable(): void
]),
);
}

public function testGetDirtyAttributesOnNewRecord(): void
{
$this->checkFixture($this->db, 'customer');

$customer = new Customer($this->db);

$this->assertSame([], $customer->getDirtyAttributes());

$customer->setAttribute('name', 'Adam');
$customer->setAttribute('email', '[email protected]');
$customer->setAttribute('address', null);

$this->assertEquals(
['name' => 'Adam', 'email' => '[email protected]', 'address' => null],
$customer->getDirtyAttributes()
);
$this->assertEquals(
['email' => '[email protected]', 'address' => null],
$customer->getDirtyAttributes(['id', 'email', 'address', 'status', 'unknown']),
);

$this->assertTrue($customer->save());
$this->assertSame([], $customer->getDirtyAttributes());

$customer->setAttribute('address', '');

$this->assertSame(['address' => ''], $customer->getDirtyAttributes());
}

public function testGetDirtyAttributesAfterFind(): void
{
$this->checkFixture($this->db, 'customer');

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

$this->assertSame([], $customer->getDirtyAttributes());

$customer->setAttribute('name', 'Adam');
$customer->setAttribute('email', '[email protected]');
$customer->setAttribute('address', null);

$this->assertEquals(
['name' => 'Adam', 'email' => '[email protected]', 'address' => null],
$customer->getDirtyAttributes(),
);
$this->assertEquals(
['email' => '[email protected]', 'address' => null],
$customer->getDirtyAttributes(['id', 'email', 'address', 'status', 'unknown']),
);
}

public function testGetDirtyAttributesWithProperties(): void
{
$this->checkFixture($this->db, 'customer');

$customer = new CustomerWithProperties($this->db);
$this->assertSame([
'name' => null,
'address' => null,
], $customer->getDirtyAttributes());

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

$this->assertSame([], $customer->getDirtyAttributes());

$customer->setEmail('[email protected]');
$customer->setName('Adam');
$customer->setAddress(null);
$customer->setStatus(null);

$this->assertEquals(
['email' => '[email protected]', 'name' => 'Adam', 'address' => null, 'status' => null],
$customer->getDirtyAttributes(),
);
$this->assertEquals(
['email' => '[email protected]', 'address' => null],
$customer->getDirtyAttributes(['id', 'email', 'address', 'unknown']),
);
}
}
79 changes: 79 additions & 0 deletions tests/Stubs/ActiveRecord/CustomerWithProperties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

namespace Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord;

use Yiisoft\ActiveRecord\ActiveQuery;
use Yiisoft\ActiveRecord\ActiveRecord;

/**
* Class Customer with defined properties.
*/
class CustomerWithProperties extends ActiveRecord
{
protected int $id;
protected string $email;
protected string|null $name = null;
public string|null $address = null;

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

public function getId(): int
{
return $this->id;
}

public function getEmail(): string
{
return $this->email;
}

public function getName(): string|null
{
return $this->name;
}

public function getAddress(): string|null
{
return $this->address;
}

public function getStatus(): int|null
{
return $this->getAttribute('status');
}

public function getProfile(): ActiveQuery
{
return $this->hasOne(Profile::class, ['id' => 'profile_id']);
}

public function getOrders(): ActiveQuery
{
return $this->hasMany(Order::class, ['customer_id' => 'id'])->orderBy('[[id]]');
}

public function setEmail(string $email): void
{
$this->email = $email;
}

public function setName(string|null $name): void
{
$this->name = $name;
}

public function setAddress(string|null $address): void
{
$this->address = $address;
}

public function setStatus(int|null $status): void
{
$this->setAttribute('status', $status);
}
}
Loading