This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

viral-vector
viral-vector

Simple question here. If i define a has many relation & utilize the relation manager. How can i override the query that populates the relation manager list. I would like to eager load some data saving me the (N) queries the system tries to run on all objects on the list.

Scott
Scott

Use relationExtendQuery in your controller

class Things extends Controller
{
    public $implements = [
        'Backend.Behaviors.RelationController',
    ];

    public function relationExtendQuery($query)
    {
        $query->with('whatever');
    }
}
viral-vector
viral-vector

@Scott. Thank you for the reply.

I thought about using that function. But "!!!! WARNING: DO NOT USE - This method is scheduled to be removed" has kept me away from using it. &

Scott
Scott

Ah that's right, I forgot about that. Try extending the query from your controller like this instead...

public function relationExtendViewWidget($widget, $field)
{
    $widget->bindEvent('list.extendQuery', function($query) { 
        $query->with('whatever');
    });
}
viral-vector
viral-vector

"relationExtendViewWidget". The bind event is registered, but the query parameter is not applied. I exposed ($widget) with dd($widget) & noticed that the closure passed to bindEvent is registered. But the query parameter is null & no change occurs to the list. For example

        public function relationExtendViewWidget($widget, $field)
        {
                     $widget->bindEvent('list.extendQuery', function($query) { 
                            $query->where('id', '=', 10000);
                     });
         }

Still returns the full list..not sure why October is registering the closure on boot, but ignoring it once it actually queries the DB

KurtJensen
KurtJensen

This is what I have found to work:

public function relationExtendManageWidget($widget, $field) {
                 $widget->bindEvent('list.extendQuery', function($query) { 
                        $query->where('category', '=', 10000);
                 });
}

Last updated

1-6 of 6

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.