Doctrine and symfony filter, debug the filter

2019-08-31 09:01发布

问题:

So i have the following filter:

class ClientFilter extends SQLFilter
{
    public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias)
    {
        return $targetTableAlias.'client_id = '. $this->getParameter('client_id');
    }

}

and in my controller:

$em = $this->get('doctrine.orm.default_entity_manager');
$filter = $em->getFilters()->enable('client_filter');
var_dump($em->getFilters()->isEnabled('client_filter'));

it prints out bool(true) which means it is enabled, but when i look into the doctrine sql commands from symfony profiler i can't see the client_id in the WHERE statment

And i can't set it globally because i only need it after login. So the question will be, how can i check if the filter is working or not? and the other side question will be if i set it globally is there a way to only make it work after login because the user table doesnt have a client_id and it gives an error since the client id is only added after login depends on which user will login.

p.s i am adding the client_id to the session after login!

回答1:

I am answering this since i was so dump

2 things where missing in the code:

 - $filter->setParameter('client_id',1); // 1 i usually get it from session
 - and the actual call to some function to call a db table after that code i had nothing applied but the rendering of twig template.

the code in controller looks like this now:

$this->get('somerepository')->findOneById(1);