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

Fixed crash when key contains BackedEnum #183

Merged
merged 1 commit into from
Jan 9, 2025

Conversation

KentarouTakeda
Copy link
Contributor

resolves #168, resolves: #176

Fixed an issue where relationships could not be retrieved if a composite key contained a value casted to an enum.

PHP 8.1, Laravel 8, supports the ability to cast specific column values ​​to BackedEnum and reference them from an Eloquent model.

Reference:
https://laravel.com/docs/8.x/eloquent-mutators#enum-casting

However, our library did not support this feature. If a BackedEnum value is included as part of a composite key, retrieving a relationship would crash.

  • Lazy loading of HasMany relationships
  • Eager loading of HasMany relationships
  • Eager loading of BenlongTo relationships

All of these issues are caused by our library assuming that the key value can be interpreted as a string. In addConstraints before query execution and match after execution, the key is evaluated as a string using implode or array_unique.

An attribute cast to a BackEnum has its backed value before the cast, so evaluating $backedEum->value instead of $backedEnum in the offending operation will solve this issue.

@KentarouTakeda KentarouTakeda force-pushed the backed-enum-reference branch 3 times, most recently from 0c6e6a8 to af1e392 Compare January 4, 2025 08:22
@KentarouTakeda
Copy link
Contributor Author

@topclaudy
Hi! I'm excited to find the great library. Could you please take a look at this pull request?

@topclaudy topclaudy merged commit 0e83d58 into topclaudy:master Jan 9, 2025
28 checks passed
@topclaudy
Copy link
Owner

Thanks @KentarouTakeda Released in 2.4.0

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

Successfully merging this pull request may close these issues.

Relate two enum types Error when using a column for which the model specifies cast to Enum
2 participants