Skip to content

Commit

Permalink
Merge branch 'master' into refactor-delete
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Jan 2, 2024
2 parents 18dd04a + eb86518 commit c260fb5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/BaseActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Yiisoft\Db\Helper\DbStringHelper;

use function array_combine;
use function array_diff;
use function array_flip;
use function array_intersect;
use function array_key_exists;
Expand Down Expand Up @@ -116,15 +117,12 @@ public function getAttributes(array $names = null, array $except = []): array
$names = $this->attributes();
}

/** @psalm-var list<string> $names */
foreach ($names as $name) {
/** @psalm-var mixed */
$values[$name] = $this->$name;
if ($except !== []) {
$names = array_diff($names, $except);
}

/** @psalm-var list<string> $except */
foreach ($except as $name) {
unset($values[$name]);
foreach ($names as $name) {
$values[$name] = $this->$name;
}

return $values;
Expand Down
11 changes: 11 additions & 0 deletions tests/ActiveQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2133,6 +2133,17 @@ public function testGetAttributes(): void
$this->assertEquals($attributes, $attributesExpected);
}

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

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

$attributes = $customer->findOne(1)->getAttributes(['id', 'email', 'name']);

$this->assertEquals(['id' => 1, 'email' => '[email protected]', 'name' => 'user1'], $attributes);
}

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

0 comments on commit c260fb5

Please sign in to comment.