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

Eloquent\Concerns\HasAttributes::castAttribute($key, $value) ignores $value if classCastCache is hit #43091

Closed
BreiteSeite opened this issue Jul 7, 2022 · 2 comments

Comments

@BreiteSeite
Copy link

BreiteSeite commented Jul 7, 2022

  • Laravel Version: 8.83.5
  • PHP Version: 8.1.6
  • Database Driver & Version:

Description:

\Illuminate\Database\Eloquent\Concerns\HasAttributes::castAttribute($key, $value) will ignore the provided value if it's called with the same key (but different values) multiple times.

A similar problem was reported in #33311.

I think the fix originally described there would have been the better solution, as we are facing the same problem using the owen-it/laravel-auditing library that calls \Illuminate\Database\Eloquent\Concerns\HasAttributes::castAttribute($key, $value).

When making multiple calls to castAttribute() with the same key but different values, the first value wins as this will be cached for the key. The value is not part of the cache-key.

This is problematic when attributes uses cast for ValueObjects like (for example) when using state objects.

Steps To Reproduce:

// for brevity, let's assume this Model only has one state transition recorded in audits
$modelWithStateAttribute = \App\Models\Model::where('stateAttribute', '=', 'someState')->orderBy('created_at', 'desc')->first();

$firstStateChangingAudit = $modelWithStateAttribute->audits()->whereJsonLength('new_values->stateAttribute', '>=', 0)->orderBy('created_at', 'desc')->first();

// revert the state transition
$modelWithStateRevert = $modelWithStateAttribute->transitionTo($firstStateChangingAudit, old: true);

$modelWithStateRevert will not contain the revert state but the current one because of above described issue.

Btw. my current workaround for this (specifically for States) is to remove the cache entry for state attributes before and after each function call (effectively disabling the classCastCache for state objects) by overriding the following method in my (base) model:

protected function getClassCastableAttributeValue($key, $value)
{
    if (isset($this->classCastCache[$key])) {
        $cacheClassCast = $this->classCastCache[$key];
        if ($cacheClassCast instanceof State) {
            unset($this->classCastCache[$key]);
        }
    }

    $castedValue = parent::getClassCastableAttributeValue($key, $value);

    if (isset($this->classCastCache[$key])) {
        $cacheClassCast = $this->classCastCache[$key];
        if ($cacheClassCast instanceof State) {
            unset($this->classCastCache[$key]);
        }
    }

    return $castedValue;
}
@driesvints
Copy link
Member

Heya, sorry for the late reply here.

We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as separate commits on the main/master branch and share the repository here? Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.

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

Please do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue.

Thanks!

@driesvints
Copy link
Member

I'll re-open this once a repo is provided. Thanks

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

2 participants