no 'fetched' event when model or collection is loaded by eager loader
See original GitHub issueWhen collection is loaded in scope of parent ‘load’ or withRelated it receives only ‘fetching’ event. When model is loaded in that way it receives neither ‘fetching’ nor ‘fetched’.
Script:
Photo = db.Model.extend({
tableName: 'photos',
initialize: function() {
this.on('all', function(event) {
console.log("Photo :: " + event);
};
}
});
Photos = db.Collection.extend({
model: Photo,
initialize: function() {
this.on('all', function(event) {
console.log("Photos :: " + event);
};
}
});
User = db.Model.extend({
tableName: 'users',
photos: function(){ return this.hasMany(Photo) },
other_photos: function(){ return this.hasMany(Photos) }
});
new User(id: 1).fetch(withRelated: ['photos', 'other_photos']);
Output:
Photos :: fetching
Issue Analytics
- State:
- Created 10 years ago
- Comments:10 (7 by maintainers)
Top Results From Across the Web
Eloquent event 'retrieved' is being emitted prior to eager ...
It seems that the Eloquent retrieved events are being emitted by the framework prior to loading the eager relationships.
Read more >Eager loading without empty collections - Stack Overflow
Hi, is there an easy way to eager load relationships but only if they exists? More specifically I would like to get only...
Read more >How to Eager load when there's Lazy loading involved - Medium
JOIN FETCH to the rescue ! So, let us have a look into the data first. For this example, I have created two...
Read more >Eager Loading of Related Data - EF Core - Microsoft Learn
Eager loading a collection navigation in a single query may cause performance issues. For more information, see Single vs. split queries.
Read more >Relationship Loading Techniques
Lazy loading refers to objects are returned from a query without the related objects loaded at first. When the given collection or reference...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hey @bogus34 definitely appreciate the willingness to contribute, and the various tickets you’ve opened in the last few months (and bookshelf-fields plugin)… always helpful in getting input, hope it continues. I don’t know if “berated” was really the right term, feel like Ben was being a bit dramatic there… I think everyone could just take a step back and remember, it’s just javascript 😃
I personally wouldn’t have closed the ticket, so when I saw it was closed, I just didn’t feel like reopening another ticket that has been closed before I would have liked… so I just made a mental note about it.
I tend to agree with you that the model/relation life-cycles haven’t been fully thought out. I’ve been toying around with a big overhaul of relations which drops the entire “relations” and “eager” classes entirely and instead splits each relation into its own class. Each of these classes would have a well defined interface for the fetching / loading / pairing of models, so everything is really consistent. This should also be more efficient and give much better visibility into the relation state, provides a consistent way of defining new relation types. This would also be the suitable place to introduce events associated with eager loading. So on that front, and to ditch this ticket which has gotten quite derailed, I’ve opened #253.
@bendrucker I’d prefer if tickets in both projects are kept open unless clearly closable (I don’t even think I saw the #241 you referenced as to why it’s not supported). Again, this isn’t a personal inbox, it’s my repository… it’s pretty much been my focus for about the last year and while I am super appreciative of the willingness to help, the fact is that it’s not nearly at the state of being complete. Closing issues is easy, well thought out discussion on how to improve the underlying design is more difficult and what I’d prefer.
Also let’s do less “reprimanding” of people’s tone of voice in github tickets… sarcasm, language barriers and the like are not expressed well in tickets, and while it might be poor form to not be friendly regarding oss, it’s equally as bad to react poorly… don’t feed the trolls, so to speak.
Alexander,
It’s frustrating to be berated and disrespected for software we write and support for free. Please don’t do it again.
Unless Alice is on Github, please don’t speak in hypotheticals either. It’s not helpful to us, nor to you.
There are numerous closed tickets about the challenges of firing events on collection members and relations by default. I’d suggest reading them if you’re looking to better understand this issue. We are certainly not looking to spite you. There are good reasons for the decision to not have 100% event coverage everywhere you might expect them. I’m working on better documenting and explaining why.
If you’d like to share specific use cases and your full code, we can certainly help you find the best way to implement things.