AliHichem / AliDatatableBundle

Symfony2 Ajax Datagrid Bundle for doctrine2 entities
MIT License
112 stars 72 forks source link

Change query/where #128

Closed j0ssGZ closed 9 years ago

j0ssGZ commented 9 years ago

Hello!

For my application I need to show users that are inactive when I check a checkbox and but consistently fails :(.

My function is to mark a check sent to the same path to get himself a parameter with the option for an "if" change consultation . This is where I get lost somehow and simple enough, although the consultations are going well ( tested with "die($qb)" consultation comes correct in each case) never prints the table with a result of where.

Testing I've noticed that this happens when there are more than one possible query in the " _datatable ()" function (although never enter the two ifs) . If I leave only one query as ie ​​... " -> where ( ' x.isActive = false ' ) " it works right, so the problem is not in the query itself.

Controller code:

private function _datatable()
{
    $opt = $this->getRequest()->query->get('opt') == 'true'? true : false;        
    $datatable = $this->get('datatable');

    $qb = $this->getDoctrine()->getManager()->createQueryBuilder();

    if($opt != true) {
        $qb->select('x')
            ->from("XXXUserBundle:User", "x")
            ->where('x.isActive = true');
    } else {
        $qb->select('x')
            ->from("XXXUserBundle:User", "x");
    }        

    $datatable->setFields(
                        array(
                            "ID"            => 'x.id',
                            "Usuario"       => 'x.username',
                            "Nombre"        => 'x.nombre',
                            "Correo"        => 'x.email',
                            "Acciones"      => 'x.id', // Actions
                            "_identifier_"  => 'x.id')
                        )
                ->setRenderers( 
                        array(
                            2 => array(
                                'view' => 'XXXUserBundle:Renderers:_fullname.html.twig'
                            ),
                            4 => array(
                                'view' => 'XXXUserBundle:Renderers:_actions.html.twig',
                                'params' => array(
                                        'edit_route'    => 'grid_edit_user',
                                        'delete_route'  => 'grid_baja_user'
                                    ),
                            ),
                        )
                )
                ->setOrder("x.id", "desc")
                ->setHasAction(false)
                ->setSearch(true)
                ->setSearchFields(array(0,1,2,3));

    $datatable->getQueryBuilder()->setDoctrineQueryBuilder($qb);

    return $datatable;
}

Sorry if this is an issue of Symfony and not the bundle , which I doubt the truth because I am still fairly new to Symfony.

A greeting and thanks in advance.