多列原则过滤

时间:2016-11-10 19:31:46

标签: symfony doctrine-orm doctrine-query

我需要通过以下多个字段过滤学说:

SELECT company , state  
FROM employees
WHERE 
(company, state) <> ('xxxxx', 'xxxx')
AND
(company, state) <> ('xxxx', 'xxxx')
GROUP BY company, state

我尝试了以下方式:

 $qb->andWhere($qb->expr()->andX($qb->expr()->neq('b.company',"'".$i['description']."'"), $qb->expr()->neq('b.state', "'".$i['state']."'")));

但结果并不理想:

(company <> 'xxxxx' AND state <> 'xxxx') AND (company  <> 'xxxxx' AND state  <> 'xxxxx') 

我如何通过学说做第一个? 问候!

1 个答案:

答案 0 :(得分:0)

上面的查询可以作为两个列表的OR,这将使事情更简单。

E.g。

WHERE州NOT NOT州或公司NOT IN公司

如果两者都是真的那么该员工的公司+州的组合不是被排除的。

鉴于此,您可以按以下方式运行:

    $qb = $this->getEntityManager()->createQueryBuilder();

    $excludedCompanies = ['company1', 'another company', 'company etc'];
    $excludedStates = ['state1', 'another state', 'state etc'];

    return $qb->select('e.company, e.state')
        ->from('YourBundle:Employee', 'e')
        ->where($qb->expr()->notIn('e.company', ':excludedCompanies'))
        ->orWhere($qb->expr()->notIn('e.state', ':excludedStates'))
        ->setParameter('excludedCompanies', $excludedCompanies)
        ->setParameter('excludedStates', $excludedStates)
        ->groupBy('e.company')
        ->addGroupBy('e.state')
        ->getQuery()
        ->getResult()
    ;