You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
\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.
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:
protectedfunctiongetClassCastableAttributeValue($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;
}
The text was updated successfully, but these errors were encountered:
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.
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:
$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:
The text was updated successfully, but these errors were encountered: