Eager loading for SilverStripe 4

Installs: 60

Dependents: 0

Suggesters: 0

Security: 0

Stars: 4

Watchers: 2

Forks: 3

Open Issues: 2


v0.6.0 2023-03-18 08:48 UTC

This package is auto-updated.

Last update: 2024-02-19 15:10:47 UTC


Scrutinizer Code Quality Build Status Code Coverage

Attempt to solve N+1 problem in SilverStripe 4.



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.

Quick start

1. Add the following trait to all your models to use $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.

2. Declare private static $eager_loading to boost ModelAdmin's GridField output.

class YourClass extends DataObject
    private static $eager_loading = [


  • for ->with(['RelLevel1.RelLevel2']) - do not query for RelLevel1 IDs twice.
  • for ->with(['RelLevel1','RelLevel1.RelLevel2']) - do not query for RelLevel1 IDs thrice.

Reporting Issues

Please create an issue for any bugs you've found, or features you're missing.