或在哪里陈述主义1

时间:2018-07-17 07:57:12

标签: doctrine

使用一个andWhere语句解决了问题

$query->andWhere("(SELECT COUNT(*) as result FROM EvaluateurCandidature ec4 WHERE ec.candidature_id_ca = ec4.candidature_id_ca AND ec4.role_evaluateur = 'rapporteur' AND ec4.statut_evaluation = ? OR ec.candidature_id_ca = ec4.candidature_id_ca AND ec4.role_evaluateur = 'rapporteur' AND ec4.statut_evaluation = ? ) >= '2'" , array('en cours' , 'terminée'));

我尝试在查询中创建OrWhere语句,但是当我这样做时,它会返回不依赖c.id_ap的对象。

有人知道我怎么能用orWhere实现查询?我没有成功尝试过

  

$ query-> andWhere(“(SELECT COUNT(*)作为EvaluateurCandidature ec2的结果,其中ec.candidature_id_ca = ec2.candidature_id_ca AND ec2.role_evaluateur ='evaluateur'AND ec2.statut_evaluation ='en cours'。 ='terminée')> ='2'“);

我只需要检查ec2.statut_evaluation ='en cours'或'terminée'中是否有两个以上结果

这里是实际查询:

$query = Doctrine_Query::create()
            ->from('Candidature c')
            ->leftJoin('c.EvaluateurCandidature ec')
            ->where('c.id_stca = 5');

 $query->andWhere('c.id_ap=?', array($this->id_ap));

      $query->andWhere("(SELECT COUNT(*) as result FROM EvaluateurCandidature ec2 WHERE ec.candidature_id_ca = ec2.candidature_id_ca AND ec2.role_evaluateur = 'evaluateur' AND ec2.statut_evaluation = 'en cours') >= '2'");

      $query->orWhere("(SELECT COUNT(*) as result FROM EvaluateurCandidature ec3 WHERE ec.candidature_id_ca = ec3.candidature_id_ca AND ec3.role_evaluateur = 'evaluateur' AND ec3.statut_evaluation = 'terminée') >= '2'");

1 个答案:

答案 0 :(得分:0)

对于您的问题,您需要对OR条条件进行分组以避免发生此类问题,这是因为运算符优先级,ANDOR之前绑定,因此您的查询被评估为

c.id_stca = some AND  (SELECT ...) >= 2 OR (SELECT ...) >= 2

应分组为

c.id_stca = some AND ( (SELECT ...) >= 2 OR (SELECT ...) >= 2 )

对于教义1,我想您可以将查询构建器写为

$query = Doctrine_Query::create()
            ->from('Candidature c')
            ->leftJoin('c.EvaluateurCandidature ec')
            ->where('c.id_stca = 5')
            ->andWhere('c.id_ap=?', array($this->id_ap))
            ->andWhere( 
                $query->expr()->orX(
                    $query->andWhere("(SELECT COUNT(*) as result 
                                       FROM EvaluateurCandidature ec2 
                                       WHERE ec.candidature_id_ca = ec2.candidature_id_ca 
                                       AND ec2.role_evaluateur = 'evaluateur' 
                                       AND ec2.statut_evaluation = 'en cours') >= '2'"),
                    $query->andWhere("(SELECT COUNT(*) as result 
                                       FROM EvaluateurCandidature ec3 
                                       WHERE ec.candidature_id_ca = ec3.candidature_id_ca 
                                       AND ec3.role_evaluateur = 'evaluateur' 
                                       AND ec3.statut_evaluation = 'terminée') >= '2'")
                )
            );