This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
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.
Use relationExtendQuery
in your controller
class Things extends Controller
{
public $implements = [
'Backend.Behaviors.RelationController',
];
public function relationExtendQuery($query)
{
$query->with('whatever');
}
}
@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. &
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');
});
}
"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
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