BC break: Calling a method unnecessarily (#19810)

* BC: Calling a method unnecessarily

Making an indirect call to the `$this->getOldAttributes()` method caused my code to break. I understand that the correct thing would be to reference `$this->_oldAttributes`.

* Null check

* Update CHANGELOG.md

* Change isAttributeDirty() method to isValueDifferent()

* Update CHANGELOG.md
This commit is contained in:
Thiago Talma
2023-04-21 06:24:54 -03:00
committed by GitHub
parent a6ff298964
commit b77caaa0b2
2 changed files with 10 additions and 9 deletions

View File

@ -24,6 +24,7 @@ Yii Framework 2 Change Log
- Bug #19770: Fix `yii\mutex\MysqlMutex` `keyPrefix` expression param binding (kamarton) - Bug #19770: Fix `yii\mutex\MysqlMutex` `keyPrefix` expression param binding (kamarton)
- Enh #19794: Add caching in `yii\web\Request` for `getUserIP()` and `getSecureForwardedHeaderTrustedParts()` (rhertogh) - Enh #19794: Add caching in `yii\web\Request` for `getUserIP()` and `getSecureForwardedHeaderTrustedParts()` (rhertogh)
- Bug #19795: Fix `yii\web\Response::redirect()` to prevent setting headers with URL containing new line character (bizley) - Bug #19795: Fix `yii\web\Response::redirect()` to prevent setting headers with URL containing new line character (bizley)
- Enh #19804: Remove the unnecessary call to `$this->oldAttributes` in `BaseActiveRecord::getDirtyAttributes()` (thiagotalma)
2.0.47 November 18, 2022 2.0.47 November 18, 2022
------------------------ ------------------------

View File

@ -639,7 +639,7 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
} }
} else { } else {
foreach ($this->_attributes as $name => $value) { foreach ($this->_attributes as $name => $value) {
if (isset($names[$name]) && (!array_key_exists($name, $this->_oldAttributes) || $this->isAttributeDirty($name, $value))) { if (isset($names[$name]) && (!array_key_exists($name, $this->_oldAttributes) || $this->isValueDifferent($value, $this->_oldAttributes[$name]))) {
$attributes[$name] = $value; $attributes[$name] = $value;
} }
} }
@ -1756,18 +1756,18 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
} }
/** /**
* @param string $attribute * @param mixed $newValue
* @param mixed $value * @param mixed $oldValue
* @return bool * @return bool
* @since 2.0.48
*/ */
private function isAttributeDirty($attribute, $value) private function isValueDifferent($newValue, $oldValue)
{ {
$old_attribute = $this->oldAttributes[$attribute]; if (is_array($newValue) && is_array($oldValue)) {
if (is_array($value) && is_array($this->oldAttributes[$attribute])) { $newValue = ArrayHelper::recursiveSort($newValue);
$value = ArrayHelper::recursiveSort($value); $oldValue = ArrayHelper::recursiveSort($oldValue);
$old_attribute = ArrayHelper::recursiveSort($old_attribute);
} }
return $value !== $old_attribute; return $newValue !== $oldValue;
} }
} }