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

Fixed issue when trying to check if a multidimensional array is dirty… #19272

18 changes: 17 additions & 1 deletion framework/db/BaseActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ public function getDirtyAttributes($names = null)
}
} else {
foreach ($this->_attributes as $name => $value) {
if (isset($names[$name]) && (!array_key_exists($name, $this->_oldAttributes) || $value !== $this->_oldAttributes[$name])) {
if (isset($names[$name]) && (!array_key_exists($name, $this->_oldAttributes) || $this->isAttributeDirty($name, $value))) {
$attributes[$name] = $value;
}
}
Expand Down Expand Up @@ -1760,4 +1760,20 @@ private function setRelationDependencies($name, $relation, $viaRelationName = nu
$this->setRelationDependencies($name, $viaQuery, $viaRelationName);
}
}

/**
* @param string $attribute
* @param mixed $value
* @return bool
*/
private function isAttributeDirty($attribute, $value)
{
$old_attribute = $this->oldAttributes[$attribute];
if (is_array($value) && is_array($this->oldAttributes[$attribute])) {
ArrayHelper::sortMultidimensionalArray($value);
ArrayHelper::sortMultidimensionalArray($old_attribute);
}

return $value !== $old_attribute;
samdark marked this conversation as resolved.
Show resolved Hide resolved
}
}
15 changes: 15 additions & 0 deletions framework/helpers/BaseArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -999,4 +999,19 @@ public static function filter($array, $filters)

return $result;
}

/**
* Sorts multidimensional array
* @param $array
* @return void
*/
public static function sortMultidimensionalArray(&$array)
samdark marked this conversation as resolved.
Show resolved Hide resolved
{
ksort($array);
samdark marked this conversation as resolved.
Show resolved Hide resolved
foreach ($array as &$item) {
if (is_array($item)) {
$item = self::sortMultidimensionalArray($item);
}
}
}
Copy link
Contributor

@WinterSilence WinterSilence Feb 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return $array; to using as callback for array_* functions

}