Skip to content

Commit

Permalink
Fix #20191: Fix ActiveRecord::getDirtyAttributes() for JSON columns…
Browse files Browse the repository at this point in the history
… with multi-dimensional array values
  • Loading branch information
brandonkelly authored Jun 8, 2024
1 parent 3fa2d61 commit 048eef4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
12 changes: 9 additions & 3 deletions framework/db/BaseActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions tests/framework/db/BaseActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
[],
Expand Down

0 comments on commit 048eef4

Please sign in to comment.