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

Support ObservedBy on parent model classes #53579

Merged
merged 5 commits into from
Nov 19, 2024
Merged

Conversation

adamthehutt
Copy link
Contributor

When an Eloquent model extends another Eloquent model, the check for the ObservedBy attribute only considers the class itself. Any observers registered in the parent model are ignored.

This change adds support for the ObservedBy attribute in parent classes and traverses the hierarchy until the top Model class is reached.

Copy link

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@taylorotwell
Copy link
Member

Hey there! Could we get a test for this? Also should it keep going back up the tree in case there are multiple extensions?

@taylorotwell taylorotwell marked this pull request as draft November 19, 2024 19:43
@adamthehutt
Copy link
Contributor Author

@taylorotwell You're too fast! :) I added another couple of commits after the initial draft pull request. These address the tree traversal issue you're asking about.

Will add a test ASAP.

@adamthehutt
Copy link
Contributor Author

@taylorotwell Added a test. (Sorry about the sloppy commit history.)

@adamthehutt adamthehutt marked this pull request as ready for review November 19, 2024 20:10
@taylorotwell taylorotwell merged commit 11508cd into laravel:11.x Nov 19, 2024
31 checks passed
@taylorotwell
Copy link
Member

Thanks!

@patrickomeara
Copy link
Contributor

This is a breaking change for me as I have a parent observer that creates polymorphic relationships, the grandchild model can't have these and doesn't have a morph map value.

Here is another example:

class MessageObserver
{
    public function created(Message $message) 
    {
        // send to all users
    }
}

#[ObservedBy(MessageObserver)]
class Message extends Model {}

class AdminMessage extends Message {}

Previously, creating an AdminMessage wouldn't send a notification to all users, now it is.

This PR takes away the granularity of adding the ObservedBy attribute where it's needed and assumes it can run for all child models.

Should we check for instanceOf in the observer methods?

@taylorotwell
Copy link
Member

taylorotwell commented Nov 22, 2024

@patrickomeara hmm, tough call. I would say that the change we made is in line with the way other things work in the framework in general. For example, putting ShouldQueue on a parent class and dispatching the child class to the Bus will in fact queue it. Same with mail, notifications, etc. Or, registering a container callback to a parent type will get called for child types.

@patrickomeara
Copy link
Contributor

@taylorotwell OK, that makes sense. Staying in line across the framework is the correct approach. In my case I will remove the inheritance and use the same table name. It's a hangover from a long time ago and feels like an anti-pattern now.

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.

3 participants