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

[9.x] Refactor for missing attributes #44876

Closed
wants to merge 11 commits into from

Conversation

bert-w
Copy link
Contributor

@bert-w bert-w commented Nov 8, 2022

The changes below allow developers to use the traits HasAttributes, RestrictsAttributes, UsesTimestamps themselves in custom classes, therefore creating a model with a subset of components. For example, I am using this in my own project to create a Country model disconnected from a database (the country attributes are loaded in-code since it never changes in my situation) while still allowing all the handy stuff from HasAttributes like mutators.

  • Moved functionality of [9.x] Opt-in Model::preventAccessingMissingAttributes() option #44283 (Opt-in Model::preventAccessingMissingAttributes() option) to a new trait RestrictsAttributes instead of it being spread out in HasAttributes and Illuminate\Database\Eloquent\Model. This increases modularity greatly and allows developers to use these traits themselves.

  • Changed HasAttributes trait so it has no dependencies on the RestrictsAttributes trait (that is: functions from this trait have to be checked first using method_exists() before being called. Not having the RestrictsAttributestrait thus reverts the behaviour to before PR [9.x] Opt-in Model::preventAccessingMissingAttributes() option #44283 ).

  • Changed HasAttributes trait to have no dependencies on the UsesTimestamps trait.

  • Moved __get() and __set() from Eloquent\Model to the very top of the HasAttributes trait since it calls exactly those functions contained in that trait.

  • Changed the order of the if-statement (a && b && c) in throwMissingAttributeExceptionIfApplicable() so it doesn't break on the calls $this->exists && !$this->wasRecentlyMerged since these properties are not part of the trait (and therefore may be undefined depending on where you include this trait). Now, the self::preventsAccessingMissingAttributes() is called first which by default returns false therefore ending the if-statement. This only increases stability since a && b && c can be rearranged in any way, but in this situation there is 1 case that is better being called first.

  • Included tests to show that these traits can be used mostly independently. There are still some dependencies from HasAttributes to HasRelations left in the code (and probably some other traits as well) but I have not included those changes in this PR.

@bert-w bert-w changed the title Fix missing attributes exception [9.x] Refactor for missing attributes Nov 8, 2022
@bert-w bert-w marked this pull request as ready for review November 8, 2022 15:07
@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If possible, please consider releasing your code as a package so that the community can still take advantage of your contributions!

If you feel absolutely certain that this code corrects a bug in the framework, please "@" mention me in a follow-up comment with further explanation so that GitHub will send me a notification of your response.

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.

2 participants