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

Fix #20191 #20192

Merged
merged 4 commits into from
Jun 8, 2024
Merged
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
@@ -4,6 +4,7 @@ Yii Framework 2 Change Log
2.0.51 under development
------------------------

- Bug #20191: Fix `ActiveRecord::getDirtyAttributes()` for JSON columns with multi-dimensional array values (brandonkelly)
- Bug #20175: Fix bad result for pagination when used with GridView (@lav45)


12 changes: 9 additions & 3 deletions framework/db/BaseActiveRecord.php
Original file line number Diff line number Diff line change
@@ -1781,9 +1781,15 @@ private function setRelationDependencies($name, $relation, $viaRelationName = nu
*/
private function isValueDifferent($newValue, $oldValue)
{
if (is_array($newValue) && is_array($oldValue) && ArrayHelper::isAssociative($oldValue)) {
$newValue = ArrayHelper::recursiveSort($newValue);
$oldValue = ArrayHelper::recursiveSort($oldValue);
if (is_array($newValue) && is_array($oldValue)) {
// Only sort associative arrays
$sorter = function(&$array) {
if (ArrayHelper::isAssociative($array)) {
ksort($array);
}
};
$newValue = ArrayHelper::recursiveSort($newValue, $sorter);
$oldValue = ArrayHelper::recursiveSort($oldValue, $sorter);
}

return $newValue !== $oldValue;
4 changes: 4 additions & 0 deletions tests/framework/db/BaseActiveRecordTest.php
Original file line number Diff line number Diff line change
@@ -28,6 +28,10 @@ public function provideArrayValueWithChange()
['pineapple' => 2, 'apple' => 5, 'banana' => 1],
['pineapple' => 2, 'apple' => 3, 'banana' => 1],
],
'multi-dimensional array' => [
['foo' => ['c', 'b', 'a']],
['foo' => ['b', 'c', 'a']],
],

'filling an empty array' => [
[],
Loading