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

Possible bug on model Accessors on new release #43735

Closed
caiokawasaki opened this issue Aug 17, 2022 · 13 comments
Closed

Possible bug on model Accessors on new release #43735

caiokawasaki opened this issue Aug 17, 2022 · 13 comments

Comments

@caiokawasaki
Copy link
Contributor

caiokawasaki commented Aug 17, 2022

  • Laravel Version: 9.25.1
  • PHP Version: 8.1.8
  • Database Driver & Version: mysql ('mysql/mysql-server:8.0')

Description:

Today I ran composer update in my application, the Framework version got updated to 9.25.1, the previous version was 9.23.0.

This was working before on version 9.23.0:

public function totalValue(): Attribute
{
    $receivedMessages = $this->total_received_messages * $this->environment->singleReceivedMessageValue();
    $sentMessages = $this->total_sent_messages * $this->environment->singleSentMessageValue();

    return new Attribute(
        get: fn($value) => MoneyService::format(
            value: $receivedMessages + $sentMessages
        )
    );
}

I got this in the log after the update:

Call to a member function singleReceivedMessageValue() on null

I didn't understand why this was happening and it took me a while to find the problem.

However, if I change the accessor to the following everything works normally:

public function totalValue(): Attribute
{
    return new Attribute(
        get: fn($value) => MoneyService::format(
            value: $this->total_received_messages * $this->environment->singleReceivedMessageValue() +
            $this->total_sent_messages * $this->environment->singleSentMessageValue()
        )
    );
}

On version 9.25.1 this accessor is not working, looks like the acessor can't access relations outside the return new Attribute function, is this right? Or is it a bug?

@morloderex
Copy link
Contributor

@caiokawasaki What is the value of $this->environment if it is supposed to be a relation, then this would make a lot of sense. Sense the environment relation could potentially not be available at the time the totalValue needs to be computed.

@caiokawasaki
Copy link
Contributor Author

@morloderex the relation is being eagger loadded, isn't it strange that I can access the relation only inside the Attribute function?

@taylorotwell
Copy link
Member

I think we need more context here. Like a full route example that is broken.

@taylorotwell
Copy link
Member

@driesvints pointed out this PR: #43554

But, it's not clear to me atm how that PR would cause the behavior you describe.

@pintend
Copy link
Contributor

pintend commented Aug 21, 2022

I wonder if this is related #42537 in my testing there is something funny going with with object caching

@driesvints
Copy link
Member

@serpentblade any ideas here? If we can't figure this one out it's probably best that we revert your PR.

@ollieread
Copy link
Contributor

@caiokawasaki could you dump your WHOLE model in a gist, please?

@serpentblade
Copy link
Contributor

serpentblade commented Aug 22, 2022

@serpentblade any ideas here? If we can't figure this one out it's probably best that we revert your PR.

My PR was related to casts. The major change was the inclusion of the new casts cache to reduce repeated calls to cast type name conversions. However, that's not to say it can't somehow be involved.

A stack trace for the error would be helpful to understand the full context of when the accessor is being called in the model's lifecycle. There are a number of places where the accessor method is called and getting a full trace would help identify which path is the problem.

IMO, the "working" code identified in the original post is the correct way to implement attribute accessors, as you want the evaluation of the accessor to occur when Attribute::get() is called and NOT when the Attribute accessor is first defined.

@caiokawasaki were any other packages besides laravel/framework updated at the same time?

@Tofandel
Copy link
Contributor

Tofandel commented Aug 22, 2022

I also fail to see how the casts PR would have any effect on this, I'm also enclined to say that it may be another package update.

Where is this environment property coming from?

It does seem to be a similar issue as #42537 because OP is using $this->environment in the attribute getter, in which case laravel version shouldn't matter (but it might as well be totally different if environment doesn't use a magic getter)
#42537 (comment)

@driesvints
Copy link
Member

@caiokawasaki can you please provide a repo that reproduces this?

laravel new bug-report --github="--public"

@caiokawasaki
Copy link
Contributor Author

Sorry for the delay, I ended up running out of internet in the last few days, I'll provide a repository with the problem described, in a few minutes I'll send the repository here.

@caiokawasaki
Copy link
Contributor Author

https://github.com/caiokawasaki/laravel-bug-report

I recreated the code in a new repository, added all the dependencies from the original project and still didn't get the problem, I'll dig deeper to try to find out what might be going on in my project.

@driesvints
Copy link
Member

Feel free to provide a repo once you managed to recreate it 👍

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

No branches or pull requests

8 participants