From adadbefe885589ea1a409346c6f36d6bc20e863b Mon Sep 17 00:00:00 2001 From: Winus van Heumen <76947759+wvanheumen@users.noreply.github.com> Date: Fri, 15 Jul 2022 22:52:39 +0200 Subject: [PATCH] Fix #19469: Fix a virtual relation not working because of new isset checks in `\yii\db\ActiveRelationTrait` --- framework/CHANGELOG.md | 1 + framework/db/ActiveRelationTrait.php | 4 ++-- tests/data/ar/Order.php | 8 ++++++++ tests/framework/db/ActiveRecordTest.php | 11 +++++++++++ 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 0917bfba92..57488ad7f4 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -4,6 +4,7 @@ Yii Framework 2 Change Log 2.0.46 under development ------------------------ +- Bug #19469: Fix a virtual relation not working because of new isset checks in `\yii\db\ActiveRelationTrait` (wvanheumen) - Bug #19380: Fix PHP 8.1 passing non string to trim() in `yii\db\Query` (wa1kb0y) - Bug #19272: Fix bug in dirty attributes check on multidimensional array (speedplli) - Bug #19349: Fix PHP 8.1 error when attribute and label of `yii\grid\DataColumn` are empty (githubjeka) diff --git a/framework/db/ActiveRelationTrait.php b/framework/db/ActiveRelationTrait.php index 597c8c3d99..487e8e55f6 100644 --- a/framework/db/ActiveRelationTrait.php +++ b/framework/db/ActiveRelationTrait.php @@ -528,7 +528,7 @@ trait ActiveRelationTrait // single key $attribute = reset($this->link); foreach ($models as $model) { - $value = isset($model[$attribute]) ? $model[$attribute] : null; + $value = isset($model[$attribute]) || (is_object($model) && property_exists($model, $attribute)) ? $model[$attribute] : null; if ($value !== null) { if (is_array($value)) { $values = array_merge($values, $value); @@ -586,7 +586,7 @@ trait ActiveRelationTrait { $key = []; foreach ($attributes as $attribute) { - if (isset($model[$attribute])) { + if (isset($model[$attribute]) || (is_object($model) && property_exists($model, $attribute))) { $key[] = $this->normalizeModelKey($model[$attribute]); } } diff --git a/tests/data/ar/Order.php b/tests/data/ar/Order.php index 6fbc5b0fde..19a5b7a58c 100644 --- a/tests/data/ar/Order.php +++ b/tests/data/ar/Order.php @@ -25,6 +25,8 @@ class Order extends ActiveRecord { public static $tableName; + public $virtualCustomerId = null; + public static function tableName() { return static::$tableName ?: 'order'; @@ -238,4 +240,10 @@ class Order extends ActiveRecord { return $this->hasMany(Item::className(), ['id' => 'item_id'])->via('orderItemsFor8'); } + + public function getVirtualCustomer() + { + return $this->hasOne(Customer::className(), ['id' => 'virtualCustomerId']); + } + } diff --git a/tests/framework/db/ActiveRecordTest.php b/tests/framework/db/ActiveRecordTest.php index 3881be6175..03bdd0ef68 100644 --- a/tests/framework/db/ActiveRecordTest.php +++ b/tests/framework/db/ActiveRecordTest.php @@ -2159,4 +2159,15 @@ abstract class ActiveRecordTest extends DatabaseTestCase 'item_id' => null, ])); } + + public function testVirtualRelation() + { + /* @var $orderClass ActiveRecordInterface */ + $orderClass = $this->getOrderClass(); + $order = $orderClass::findOne(2); + $order->virtualCustomerId = $order->customer_id; + + $this->assertNotNull($order->virtualCustomer); + } + }