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() (without breaking BC) #20290

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
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: 7 additions & 0 deletions framework/db/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,9 @@ protected static function filterValidColumnNames($db, array $aliases)

/**
* {@inheritdoc}
*
* @param array $with If an array is passed to this function,
* it will be interpreted as an array of relations to eagerly load for the refreshed record.
*/
public function refresh()
{
Expand All @@ -290,6 +293,10 @@ public function refresh()
foreach ($this->getPrimaryKey(true) as $key => $value) {
$pk[$tableName . '.' . $key] = $value;
}
$args = func_get_args();
if (array_key_exists(0, $args) && is_array($args[0])) {
$query->with(...$args[0]);
}
$query->where($pk);

/* @var $record BaseActiveRecord */
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