diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 63c4e21ff1..93426f7cd0 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -3,7 +3,7 @@ Yii Framework 2 Change Log 2.0.14 under development ------------------------ - +- Bug #15522: Fixed `yii\db\ActiveRecord::refresh()` method does not use an alias in the condition (vladis84) - Enh #15476: Added `\yii\widgets\ActiveForm::$validationStateOn` to be able to specify where to add class for invalid fields (samdark) - Enh #13996: Added `yii\web\View::registerJsVar()` method that allows registering JavaScript variables (Eseperio, samdark) - Enh #9771: Assign hidden input with its own set of HTML options via `$hiddenOptions` in activeFileInput `$options` (HanafiAhmat) diff --git a/framework/db/ActiveRecord.php b/framework/db/ActiveRecord.php index 122e5c9d04..babff644d1 100644 --- a/framework/db/ActiveRecord.php +++ b/framework/db/ActiveRecord.php @@ -194,13 +194,17 @@ class ActiveRecord extends BaseActiveRecord */ public function refresh() { + $query = static::find(); + $tableName = key($query->getTablesUsedInFrom()); $pk = []; // disambiguate column names in case ActiveQuery adds a JOIN foreach ($this->getPrimaryKey(true) as $key => $value) { - $pk[static::tableName() . '.' . $key] = $value; + $pk[$tableName . '.' . $key] = $value; } + $query->where($pk); + /* @var $record BaseActiveRecord */ - $record = static::findOne($pk); + $record = $query->one(); return $this->refreshInternal($record); } diff --git a/tests/data/ar/CustomerWithAlias.php b/tests/data/ar/CustomerWithAlias.php new file mode 100644 index 0000000000..a5a31c5c47 --- /dev/null +++ b/tests/data/ar/CustomerWithAlias.php @@ -0,0 +1,40 @@ +alias('csr'); + return $activeQuery; + } +} diff --git a/tests/framework/ar/ActiveRecordTestTrait.php b/tests/framework/ar/ActiveRecordTestTrait.php index e74f6fab3b..6bcb0aa200 100644 --- a/tests/framework/ar/ActiveRecordTestTrait.php +++ b/tests/framework/ar/ActiveRecordTestTrait.php @@ -275,6 +275,16 @@ trait ActiveRecordTestTrait $this->assertEquals('user1', $customer->name); } + public function testRefresh_querySetAlias_findRecord() + { + $customer = new \yiiunit\data\ar\CustomerWithAlias(); + $customer->id = 1; + + $customer->refresh(); + + $this->assertEquals(1, $customer->id); + } + public function testEquals() { /* @var $customerClass \yii\db\ActiveRecordInterface */