-
Notifications
You must be signed in to change notification settings - Fork 824
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
EagerLoadedList
can't filter or sort by fields on its relations
#10874
Comments
I’ve been using https://github.com/gurucomkz/silverstripe-eagerloading/ in a project recently where I needed to eager load relations with a filter applied, e.g. product has_many attributes, eager load attributes where If we’re chaining like Do you think an API like this would be possible? $products = $products->eagerLoad('Attributes', function (EagerLoadedDataList $attributes) {
return $attributes->filter('Featured', true)->sort('Title', 'ASC');
}); I’m not sure how it would work for chained relations, I think it’d probably have to be something like: $groups = $groups->eagerLoad('Members.FavouriteProducts', function (EagerLoadedDataList $products) {
// $products would be favourited products for *all* members in $groups
});
$groups = $groups->eagerLoad('Members', function (EagerLoadedDataList $members) {
return $members->filter('FirstName', 'Bob');
})->eagerLoad('Members.FavouriteProducts', function (EagerLoadedDataList $products) {
// $products would be favourited products for only members named Bob in $groups
}); |
That sounds really complex, not just to implement but also to use... That sort of thing could be possible down the line, but I don't think it's in scope for the current iteration. That said, if you take a look at what's been implemented so far in both the linked pr and the eager loading that's already on the 5 branch and you see a clear way forward, then it could definitely be worth exploring. |
Just to be a little bit more specific about why the recommended additional API would be out of scope for this iteration: Regardless of whether we have some mechanism for pre-sorting and pre-filtering, the new |
EagerLoadedRelationList
can't filter or sort by fields on its relationsEagerLoadedDataList
can't filter or sort by fields on its relations
That all makes sense, I appreciate that you’re actually doing the work here and I’m just spouting ideas 😛 |
EagerLoadedDataList
can't filter or sort by fields on its relationsEagerLoadedList
can't filter or sort by fields on its relations
I've come to the conclusion that trying to filter by relations post-eagerloading is not feasible in a way that isn't likely to blow away any optimisation the eager loading provides. |
#10864 introduces a new
EagerLoadedList
which mimicsDataList
funtionality, but with a dataset that has already been fetched from the database.Currently that list doesn't support sorting or filtering by relation fields, e.g.
The
sort()
implementation currently does check if relations are valid, but then throws an exception because it can't actually sort by it.filter()
currently ignores them entirely.Acceptance Criteria
See #10874 (comment)
Can sort by relations, the same as with theDataList
relation listsCan filter/exclude by relations, the same as with theDataList
relation listsIf feasible, filtering by collations is also implemented (e.g.$list->filter('Relation.Count()', 5)
)Documentation is updated to remove any references to this not being possible.Notes
Possibly filtering will be implemented in this issue instead (which is in progress) - Static lists likeArrayList
orEagerLoadedDataList
should support SearchFilter syntax for filter/exclude #5911$this->eagerLoadedData
- we should avoid making any new db queries to fetch relations we've already loaded.PRs
The text was updated successfully, but these errors were encountered: