Symfony createQueryBuilder oparator orX

时间:2016-01-26 17:10:49

标签: php symfony doctrine-orm dql

我的queryBuilder中有这个部分的问题。我需要选择开发人员的参数

        $qb
        ->andWhere($qb->expr()->like('u.roles', $qb->expr()->literal('%'.'ROLE_FREELANCER'.'%')))
        ->orWhere($qb->expr()->like('u.roles', $qb->expr()->literal('%'.'ROLE_DEVELOPER'.'%')));

我要替换 通过

->andWhere($qb->expr()->orX('')))

但有很多错误,怎么样?

    public function notificationProject($paramFetcher)
{
    $em = $this->getEntityManager();

    $qb = $em->createQueryBuilder();

    $qb
        ->from('ArtelProfileBundle:Users', 'u')
        ->select('u.id, u.email as u_email, m.email, d.skills, u.roles, m.unsubscribeDate as uns_date')
        ->addSelect("IF(u.roles LIKE '%ROLE_DEVELOPER%', m.email, u.email) as send_email")

        ->leftJoin('u.developer', 'd') //this works assuming the doctrine mappings are correct on the $developer property in the ArtelProfileBundle:Users' entity
        ->leftJoin('d.teams', 't')
        ->leftJoin('t.users', 'm')
        ->where('u.unsubscribeDate <= :today');
    foreach ($paramFetcher[1] as $skill) {
        if ($skill) {
            $qb
            ->andWhere($qb->expr()->like('d.skills', $qb->expr()->literal('%"'.$skill.'"%')));
        }
    }
    $qb
        ->andWhere($qb->expr()->like('u.roles', $qb->expr()->literal('%'.'ROLE_FREELANCER'.'%')))
        ->orWhere($qb->expr()->like('u.roles', $qb->expr()->literal('%'.'ROLE_DEVELOPER'.'%')));

    foreach ($paramFetcher[0] as $tag) {
        if ($tag) {
            $qb
            ->andWhere($qb->expr()->notlike('d.tags', $qb->expr()->literal('%"'.$tag.'"%')));
        }
    }
    $qb
        ->andWhere($qb->expr()->orX('m.unsubscribeDate <= :today', $qb->expr()->isNull('m.unsubscribeDate')))
        ->groupBy('send_email')
        ->setParameter('today', new \DateTime());
    $entities = $qb->getQuery()->getArrayResult();
}

我有这个_dql

SELECT u.id, u.email as u_email, m.email, d.skills, u.roles, m.unsubscribeDate as uns_date, IF(u.roles LIKE '%ROLE_DEVELOPER%', m.email, u.email) as send_email FROM ArtelProfileBundle:Users u LEFT JOIN u.developer d LEFT JOIN d.teams t LEFT JOIN t.users m WHERE ((u.unsubscribeDate <= :today AND d.skills LIKE '%"ASP.NET"%' AND u.roles LIKE '%ROLE_FREELANCER%') OR u.roles LIKE '%ROLE_DEVELOPER%') AND d.tags NOT LIKE '%"blacklist"%' AND d.tags NOT LIKE '%"india"%' AND d.tags NOT LIKE '%"unsubscribed"%' AND d.tags NOT LIKE '%"bounced"%' AND (m.unsubscribeDate <= :today OR m.unsubscribeDate IS NULL) GROUP BY send_email

但我也需要这个ROLE_DEVELOPER

(u.unsubscribeDate <= :today AND d.skills LIKE '%"ASP.NET"%' AND u.roles LIKE '%ROLE_FREELANCER%') 

如果使用字段角色数组

,则使用orX代替此方法
->andWhere($qb->expr()->orX('')))

1 个答案:

答案 0 :(得分:1)

对于第一部分,它应该像这样工作:

自:

 $qb
        ->andWhere($qb->expr()->like('u.roles', $qb->expr()->literal('%'.'ROLE_FREELANCER'.'%')))
        ->orWhere($qb->expr()->like('u.roles', $qb->expr()->literal('%'.'ROLE_DEVELOPER'.'%')));

要:

->andWhere(
    $qb->expr()->orX(
        $qb->expr()->like('u.roles', $qb->expr()->literal('%'.'ROLE_FREELANCER'.'%'))
        , $qb->expr()->like('u.roles', $qb->expr()->literal('%'.'ROLE_DEVELOPER'.'%'))
    )
)

对于你的直接DQL,为什么不呢:

(u.unsubscribeDate <= :today AND d.skills LIKE '%"ASP.NET"%' AND u.roles LIKE '%ROLE_FREELANCER%')

要:

(u.unsubscribeDate <= :today AND d.skills LIKE '%"ASP.NET"%' AND (u.roles LIKE '%ROLE_FREELANCER%' OR u.roles LIKE '%ROLE_DEVELOPER%'))