From bc8b6fbb04c7dbbed2200f2ef574384716989158 Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Tue, 28 May 2024 11:20:40 +0700 Subject: [PATCH] Improve performance (#355) --- src/AbstractActiveRecord.php | 10 +++----- src/ActiveRelationTrait.php | 49 +++++++++++------------------------- 2 files changed, 19 insertions(+), 40 deletions(-) diff --git a/src/AbstractActiveRecord.php b/src/AbstractActiveRecord.php index 21ef22c02..0be18c184 100644 --- a/src/AbstractActiveRecord.php +++ b/src/AbstractActiveRecord.php @@ -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 = []; @@ -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 = []; diff --git a/src/ActiveRelationTrait.php b/src/ActiveRelationTrait.php index 5e615147e..e15d8ddec 100644 --- a/src/ActiveRelationTrait.php +++ b/src/ActiveRelationTrait.php @@ -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; @@ -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; @@ -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]; } @@ -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; @@ -635,14 +636,14 @@ 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 { @@ -650,36 +651,16 @@ private function getModelKey(ActiveRecordInterface|array $activeRecord, array $a $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), + }; } /**