Skip to content

Commit

Permalink
Improve performance (#355)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored May 28, 2024
1 parent b52db34 commit bc8b6fb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 40 deletions.
10 changes: 4 additions & 6 deletions src/AbstractActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,8 @@ public function getOldPrimaryKey(bool $asArray = false): mixed
);
}

if (count($keys) === 1) {
$key = $this->oldAttributes[$keys[0]] ?? null;

return $asArray ? [$keys[0] => $key] : $key;
if ($asArray === false && count($keys) === 1) {
return $this->oldAttributes[$keys[0]] ?? null;
}

$values = [];
Expand All @@ -209,8 +207,8 @@ public function getPrimaryKey(bool $asArray = false): mixed
{
$keys = $this->primaryKey();

if (count($keys) === 1) {
return $asArray ? [$keys[0] => $this->getAttribute($keys[0])] : $this->getAttribute($keys[0]);
if ($asArray === false && count($keys) === 1) {
return $this->getAttribute($keys[0]);
}

$values = [];
Expand Down
49 changes: 15 additions & 34 deletions src/ActiveRelationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

use Closure;
use ReflectionException;
use Stringable;
use Throwable;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidArgumentException;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\NotSupportedException;

use function array_column;
use function array_combine;
use function array_diff_key;
use function array_fill_keys;
Expand All @@ -24,7 +24,6 @@
use function count;
use function is_array;
use function is_object;
use function is_scalar;
use function is_string;
use function key;
use function reset;
Expand Down Expand Up @@ -315,7 +314,8 @@ public function populateRelation(string $name, array &$primaryModels): array
$value = [];

foreach ($keys as $key) {
$key = $this->normalizeModelKey($key);
$key = (string) $key;

if (isset($buckets[$key])) {
$value[] = $buckets[$key];
}
Expand Down Expand Up @@ -489,9 +489,10 @@ private function buildBuckets(
}

if (!$this->multiple) {
foreach ($buckets as $i => $bucket) {
$buckets[$i] = reset($bucket);
}
return array_combine(
array_keys($buckets),
array_column($buckets, 0)
);
}

return $buckets;
Expand Down Expand Up @@ -635,51 +636,31 @@ protected function filterByModels(array $models): void
$this->andWhere(['in', $attributes, $values]);
}

private function getModelKey(ActiveRecordInterface|array $activeRecord, array $attributes): false|int|string
private function getModelKey(ActiveRecordInterface|array $activeRecord, array $attributes): string
{
$key = [];

if (is_array($activeRecord)) {
foreach ($attributes as $attribute) {
if (isset($activeRecord[$attribute])) {
$key[] = $this->normalizeModelKey($activeRecord[$attribute]);
$key[] = (string) $activeRecord[$attribute];
}
}
} else {
foreach ($attributes as $attribute) {
$value = $activeRecord->getAttribute($attribute);

if ($value !== null) {
$key[] = $this->normalizeModelKey($value);
$key[] = (string) $value;
}
}
}

if (count($key) > 1) {
return serialize($key);
}

$key = reset($key);

return is_scalar($key) ? $key : serialize($key);
}

/**
* @param int|string|Stringable|null $value raw key value.
*
* @return int|string|null normalized key value.
*/
private function normalizeModelKey(int|string|Stringable|null $value): int|string|null
{
if ($value instanceof Stringable) {
/**
* ensure matching to special objects, which are convertible to string, for cross-DBMS relations,
* for example: `|MongoId`
*/
$value = (string) $value;
}

return $value;
return match (count($key)) {
0 => '',
1 => $key[0],
default => serialize($key),
};
}

/**
Expand Down

0 comments on commit bc8b6fb

Please sign in to comment.