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

BC break: Calling a method unnecessarily #19804

Merged
merged 3 commits into from
Apr 18, 2023
Merged

Conversation

thiagotalma
Copy link
Contributor

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.

Q A
Is bugfix? ✔️
New feature?
Breaks BC?
Fixed issues #19272

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`.
$old_attribute = $this->oldAttributes[$attribute];
if (is_array($value) && is_array($this->oldAttributes[$attribute])) {
$old_attribute = $this->_oldAttributes[$attribute];
if (is_array($value) && is_array($this->_oldAttributes[$attribute])) {
Copy link
Member

Choose a reason for hiding this comment

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

If I read this correctly here might be a problem when $this->_oldAttributes is null.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if (isset($names[$name]) && (!array_key_exists($name, $this->_oldAttributes) || $this->isAttributeDirty($name, $value))) {

In theory, this check ensures that this problem will never occur.

@schmunk42
Copy link
Contributor

caused my code to break.

What broke? Were there error messages, wrong data?

@yii-bot
Copy link

yii-bot commented Apr 8, 2023

Thanks for posting in our issue tracker.
In order to properly assist you, we need additional information:

  • When does the issue occur?
  • What do you see?
  • What was the expected result?
  • Can you supply us with a stacktrace? (optional)
  • Do you have exact code to reproduce it? Maybe a PHPUnit tests that fails? (optional)

Thanks!

This is an automated comment, triggered by adding the label status:need more info.

@thiagotalma
Copy link
Contributor Author

The problem was because I was overriding the _get() method.

I only opened the PR because it may be that the same problem occurs with other people. In my understanding, it makes no sense to force the call in the $this->getOldAttributes method without the slightest need, since the $this->_oldAttributes is available.

Anyway, MY problem I already solved.
Feel free to close the PR. Or improve it, because, in my opinion, that wasn't the most adequate implementation. I'm open to suggestions.

@bizley
Copy link
Member

bizley commented Apr 10, 2023

I think we could change it to use private property indeed but with additional check for null.

@thiagotalma
Copy link
Contributor Author

@bizley done

@samdark samdark added this to the 2.0.48 milestone Apr 16, 2023
@samdark
Copy link
Member

samdark commented Apr 16, 2023

Would you please a line for CHANGELOG?

@thiagotalma
Copy link
Contributor Author

@samdark done!

@bizley
Copy link
Member

bizley commented Apr 18, 2023

Sorry, just noticed that this is wrong, let's make it like that for example

private function isValueDifferent($newValue, $oldValue)
{
    if (is_array($newValue) && is_array($oldValue)) {
        $newValue = ArrayHelper::recursiveSort($newValue);
        $oldValue = ArrayHelper::recursiveSort($oldValue);
    }

    return $newValue !== $oldValue;
}

then we can just call it like

if (isset($names[$name]) && (!array_key_exists($name, $this->_oldAttributes) || $this->isValueDifferent($value, $this->_oldAttributes[$name]))) {
    $attributes[$name] = $value;
}

Otherwise we compare $value to an empty array if not set in old attributes.

Copy link
Member

@bizley bizley left a comment

Choose a reason for hiding this comment

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

Please modify as per my comment.

@samdark samdark merged commit 246a581 into yiisoft:master Apr 18, 2023
@samdark
Copy link
Member

samdark commented Apr 18, 2023

Thanks.

@samdark
Copy link
Member

samdark commented Apr 18, 2023

Owww... GitHub did not update the comments before I hit the merge button :(

@samdark
Copy link
Member

samdark commented Apr 18, 2023

Reverted commit for now. @thiagotalma would you please fix it as @bizley suggested?

samdark added a commit that referenced this pull request Apr 18, 2023
…ibutes` to avoid calling a method unnecessarily"

This reverts commit 246a581.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants