Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eager loading in ActiveRecord::refresh() (breaking BC) #20291

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Yii Framework 2 Change Log
- Enh #20279: Add to the `\yii\web\Request` `csrfHeader` property to configure a custom HTTP header for CSRF validation (olegbaturin)
- Enh #20279: Add to the `\yii\web\Request` `csrfTokenSafeMethods` property to configure a custom safe HTTP methods list (olegbaturin)
- Bug #20140: Fix compatibility with PHP 8.4: calling `session_set_save_handler()` (Izumi-kun)
- Enh #20277: Eager loading in `\yii\db\ActiveRecord::refresh` (chriscpty)

2.0.51 July 18, 2024
--------------------
Expand Down
7 changes: 5 additions & 2 deletions framework/db/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,10 @@ protected static function filterValidColumnNames($db, array $aliases)

/**
* {@inheritdoc}
*
* @param array $with If set, reloads the given relations.
*/
public function refresh()
public function refresh(array $with = [])
{
$query = static::find();
$tableName = key($query->getTablesUsedInFrom());
Expand All @@ -290,7 +292,8 @@ public function refresh()
foreach ($this->getPrimaryKey(true) as $key => $value) {
$pk[$tableName . '.' . $key] = $value;
}
$query->where($pk);
$query->where($pk)
->with(...$with);

/* @var $record BaseActiveRecord */
$record = $query->noCache()->one();
Expand Down
4 changes: 2 additions & 2 deletions framework/db/BaseActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -1082,8 +1082,8 @@ protected function refreshInternal($record)
$this->_attributes[$name] = isset($record->_attributes[$name]) ? $record->_attributes[$name] : null;
}
$this->_oldAttributes = $record->_oldAttributes;
$this->_related = [];
$this->_relationsDependencies = [];
$this->_related = $record->_related;
$this->_relationsDependencies = $record->_relationsDependencies;
$this->afterRefresh();

return true;
Expand Down
4 changes: 4 additions & 0 deletions tests/framework/ar/ActiveRecordTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,10 @@ public function testRefresh()
$customer->name = 'to be refreshed';
$this->assertTrue($customer->refresh());
$this->assertEquals('user1', $customer->name);
$this->assertFalse($customer->isRelationPopulated('profile'));

$this->assertTrue($customer->refresh(['profile']));
$this->assertTrue($customer->isRelationPopulated('profile'));
}

public function testEquals()
Expand Down
Loading