October: relationExtendQuery

Created on 15 Feb 2016  路  5Comments  路  Source: octobercms/october

    /**
     * !!!!
     * !!!! WARNING: DO NOT USE - This method is scheduled to be removed
     * !!!!
     *
     * Controller override: Extend the query used for populating the list
     * after the default query is processed.
     * @param October\Rain\Database\Builder $query
     * @param string $field
     */
    public function relationExtendQuery($query, $field)
    {
    }

Why this method is scheduled to be removed ?
It's very useful to filter relation manager list.

Although we can filter that with global events like backend.list.extendQuery, it becomes increasingly confused. In my application I have lots of list filters which I put in my Plugin.php file.

Event::listen('backend.list.extendQuery', function ($widget, $query) {
   if ($widget->model instanceof \Foo\Bar\Models\Item) {
      // Only backend users who are not super user
      if (Auth::getUser()->isSuperUser()) return;  
      $allowed = Auth::getUser()->allowedItems->lists('id');
      $query->whereIn('id', $allowed);
     }
});

Keeping filters in right controllers is much better than using global events...
It pollutes the Plugin.php file and make confusion.

class Items extends Controller {

    {...}

    public function listExtendQuery($query)
    {
            if ($this->user->isSuperUser()) return;
            $allowed = $this->user->allowedItems->lists('id');
            $query->whereIn('id', $allowed);
    }
}

What do you think ?

wontfix

Most helpful comment

Look at the relationExtendViewWidget and relationExtendManageWidget methods. These are the replacement overrides.

public function relationExtendViewWidget($widget, $field)
{
    $widget->bindEvent('list.extendQuery', function() { ... });
}

All 5 comments

You can always listen to backend.list.extendQueryBefore in the controller constructor :)

Thank you for your answer :)
But in this case why listExtendQuery still remain ? It should be removed too.

the function will be removed, as the annotations say. You will be able to extend the query using events only: backend.list.extendQuery and backend.list.extendQueryBefore. You can listen to those events in Plugin.php file or in the constructor of your desired constroller (cleaner).

Look at the relationExtendViewWidget and relationExtendManageWidget methods. These are the replacement overrides.

public function relationExtendViewWidget($widget, $field)
{
    $widget->bindEvent('list.extendQuery', function() { ... });
}

Thank you ! It's exactly what I was looking for :)

Was this page helpful?
0 / 5 - 0 ratings