如何在 Symfony2 中的 APYDataGridBundle 中过滤结果



我一直在寻找一种方法,以便在APYDataGridBundle网格应返回但找不到答案的项目列表中设置条件。

有没有办法设置 DQL 查询并将其传递给网格以显示我要获取的确切查询结果?

这是代码:

public function filteredlistAction(){
    // Create simple grid based on the entity
    $source = new Entity('ACMEBundle:MyEntity');
    // Get a grid instance
    $grid = $this->get('grid');
    // Attach the source to the grid
    $grid->setSource($source);
    ...
    ...
    **$grid->getColumns()->getColumnById('myentity_filter_column')->setData('the exact value I tried to match');**
    // Manage the grid redirection, exports and the response of the controller
    return $grid->getGridResponse('ACMEBundle:MyEntity:index_filteredlist.html.twig');
}

您可以在执行之前添加一个回调(闭包或可调用)QueryBuilder运行 - 它是这样完成的:

$source->manipulateQuery(
    function ($query)
    {
        $query->resetDQLPart('orderBy');
    }
);
$grid->setSource($source);

$queryQueryBuilder的实例,因此您可以更改所需的任何内容

此处文档中的示例

一个更"复杂"的查询。

$estaActivo = 'ACTIVO';
$tableAlias = $source->getTableAlias();
$source->manipulateQuery(
                            function ($query) use ($tableAlias, $estaActivo)
                            {
                                $query->andWhere($tableAlias . '.estado = :estaActivo')
                                      ->andWhere($tableAlias . '.tipoUsuario IN (:rol)')
                                      ->setParameter('estaActivo', $estaActivo)
                                      ->setParameter('rol', array('VENDEDOR','SUPERVISOR'), DoctrineDBALConnection::PARAM_STR_ARRAY);
                            }
                        );

干杯!

最新更新