2016-01-26 61 views
0

我在我的查詢生成器中有這部分的問題。我需要選擇的開發商Symfony createQueryBuilder oparator orX

 $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%') 

熱使用或X來代替這對於這一點,如果字段角色陣列

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

回答

1

對於冷杉t部應該工作確定這樣的:

來源:

$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%')) 
+0

Thkns,我解決了像$ orx = $ qb-> expr() - > orX(); $ orb-> add($ qb-> expr() - > like('u.roles',$ qb-> expr() - > literal('%'。'ROLE_FREELANCER'。'%'))); $ orb-> add($ qb-> expr() - > like('u.roles',$ qb-> expr() - > literal('%'。'ROLE_DEVELOPER'。'%'))); $ qb-> andWhere($ orx); 就像你的例子 –

相關問題