From c0fc4596e2ac077e596e623cb60f006a77215092 Mon Sep 17 00:00:00 2001 From: Klimov Paul Date: Fri, 24 Apr 2015 13:42:47 +0300 Subject: [PATCH] Fixed `yii\db\ActiveRelationTrait::populateRelation()` fails when `link` refers to string convertable object attribute, like `\MongoId` --- framework/CHANGELOG.md | 1 + framework/db/ActiveRelationTrait.php | 21 +++++++++++---------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 3f2fdff..1abe1ca 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -31,6 +31,7 @@ Yii Framework 2 Change Log - Bug #8082: Fixed `yii\db\BaseActiveRecord::getAttributeLabel()` return wrong label for related attribute, if several relations in chain share same name (klimov-paul) - Bug #8149: Fixed `yii\db\BaseActiveRecord::updateCounters()` fails for new record saved with counter attribute not set (klimov-paul) - Bug #8158: Avoid exception when detecting console screen size fails on windows (EliasZ) +- Bug #8165: Fixed `yii\db\ActiveRelationTrait::populateRelation()` fails when `link` refers to string convertable object attribute, like `\MongoId` (klimov-paul) - Enh #1468: Added ability to specify hints for model attributes via `attributeHints()` method (klimov-paul) - Enh #3376: Added `yii\validators\EachValidator`, which allows validation of the array attributes (klimov-paul) - Enh #6442: Improved error message on `FileHelper::createDirectory()` to include the path name of the directory (cebe) diff --git a/framework/db/ActiveRelationTrait.php b/framework/db/ActiveRelationTrait.php index 17a5e97..1139bef 100644 --- a/framework/db/ActiveRelationTrait.php +++ b/framework/db/ActiveRelationTrait.php @@ -483,19 +483,20 @@ trait ActiveRelationTrait */ private function getModelKey($model, $attributes) { - if (count($attributes) > 1) { - $key = []; - foreach ($attributes as $attribute) { - $key[] = $model[$attribute]; + $key = []; + foreach ($attributes as $attribute) { + $value = $model[$attribute]; + if (is_object($value) && method_exists($value, '__toString')) { + // ensure matching to special objects, which are convertable to string, for cross-DBMS relations, for example: `|MongoId` + $value = $value->__toString(); } - + $key[] = $value; + } + if (count($key) > 1) { return serialize($key); - } else { - $attribute = reset($attributes); - $key = $model[$attribute]; - - return is_scalar($key) ? $key : serialize($key); } + $key = reset($key); + return is_scalar($key) ? $key : serialize($key); } /**