diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 09d3066e7b5..08982f47655 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -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) diff --git a/framework/db/BaseActiveRecord.php b/framework/db/BaseActiveRecord.php index 54e5fce3ef0..10021e362cc 100644 --- a/framework/db/BaseActiveRecord.php +++ b/framework/db/BaseActiveRecord.php @@ -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; diff --git a/tests/framework/db/BaseActiveRecordTest.php b/tests/framework/db/BaseActiveRecordTest.php index 1ef9013aa77..a06fc68d992 100644 --- a/tests/framework/db/BaseActiveRecordTest.php +++ b/tests/framework/db/BaseActiveRecordTest.php @@ -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' => [ [],