如何在Symfony2中的APYDataGridBundle中筛选结果?

如何在Symfony2中的APYDataGridBundle中筛选结果?,symfony,grid,bundle,Symfony,Grid,Bundle,我一直在寻找一种方法,在APYDataGridBundle网格应该返回但找不到答案的项目列表上设置一个条件 有没有一种方法可以设置DQL查询并将其传递给网格以显示我想要获取的确切查询结果 代码如下: public function filteredlistAction(){ // Create simple grid based on the entity $source = new Entity('ACMEBundle:MyEntity'); // Get a grid

我一直在寻找一种方法,在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执行之前添加回调闭包或callable以运行—其操作如下:

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

$grid->setSource($source);
$query是QueryBuilder的一个实例,因此您可以根据需要更改任何内容


您可以在QueryBuilder执行之前添加回调闭包或callable以运行—其操作如下:

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

$grid->setSource($source);
$query是QueryBuilder的一个实例,因此您可以根据需要更改任何内容


更复杂的查询

$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'), \Doctrine\DBAL\Connection::PARAM_STR_ARRAY);
                            }
                        );

干杯

一个更复杂的查询

$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'), \Doctrine\DBAL\Connection::PARAM_STR_ARRAY);
                            }
                        );
干杯