Sonata admin bundle,如果用户没有 select 任何过滤器,如何不显示任何结果并且不对数据库执行任何查询?

Sonata admin bundle, how do not show any results and don't execute any query to DB if user don't select any filters?

我有 createQuery, configureFormFields, etc 的典型 sonata-admin 列表操作。我如何呈现标准页面,但如果用户不 select 任何过滤器,我只向他显示 "select any filter for getting results"?

我可以使用 hasFilters 检查但查询仍在执行。

{% if  admin.hasFilters() %}
    {{ parent() }}
{% else %}

我需要一些类似的东西,但不对数据库执行任何查询。

// SomeController
public function listAction()
{
    if (!$this->admin->hasFilters()) {
        return $this->renderWithExtraParams($this->admin->getTemplate('list'), [
            'action' => 'list',
            'form' => $this->admin->getDatagrid()->getForm()->createView(),
            'csrf_token' => $this->getCsrfToken('sonata.batch'),
            'export_formats' => $this->has('sonata.admin.admin_exporter') ?
                $this->get('sonata.admin.admin_exporter')->getAvailableFormats($this->admin) :
                $this->admin->getExportFormats(),
        ], null);
    }

    return parent::listAction();
}

这有点笨拙,但它正在运行。只需添加:

public function createQuery($context = 'list')
{
    $query = parent::createQuery($context);

    if (!$this->hasFilters()) {
        $query->where('1 = 0');

        return $query;
    }

    // ...
}