Attempt to solve N+1 problem in SilverStripe 4.
MyModelClass::get()->with(['Relation1','Relation2'])->filter(...);
It does not require huge configuration - only one function to be added to the query builder chain : ->with([..relations...])
.
This will result in the final DataList to be presented by the EagerLoadedDataList
class that handles the eager loading.
The module takes advantage of DataList::getGenerator()
to query for and attach the related records only when needed.
composer require gurucomkz/eagerloading
Every DataObject that has has_one/many_many/belongs_many_many which you wish to have eagerloaded must include EagerLoaderMultiAccessor
(see below).
Read the docs for full explanation.
$has_many
, $many_many
, $belongs_meny_many
:class MyClass extends DataObject {
use Gurucomkz\EagerLoading\EagerLoaderMultiAccessor;
// ...
}
If you have your own __call()
read Using with $has_many/$many_many.
private static $eager_loading
to boost ModelAdmin's GridField output.class YourClass extends DataObject
{
private static $eager_loading = [
'Relation1',
'Relation1.Relation4',
'Relation2',
'Relation3',
];
}
->with(['RelLevel1.RelLevel2'])
- do not query for RelLevel1
IDs twice.->with(['RelLevel1','RelLevel1.RelLevel2'])
- do not query for RelLevel1
IDs thrice.Please create an issue for any bugs you've found, or features you're missing.
Module rating system helping users find modules that are well supported. For more on how the rating system works visit Module standards
Score not correct? Let us know there is a problem