在教义的queryBuilder中如何做案例

时间:2019-07-24 14:12:51

标签: php doctrine-orm dql

我使用查询生成器具有以下功能:

    public function showForJustWhoCanSee() {
        $nextToSign = $this->em->createQueryBuilder();
        $nextToSign->select('u')
            ->from('HospitalApi\Entity\EletronicDocumentSignature', 's')
            ->innerJoin('HospitalApi\Entity\User', 'u', 'WITH', 's.user = u')
            ->where('s._document = :documentId')
            ->setParameter('documentId', 'ed.id')
            ->andWhere('s.signed = 0')
            ->setMaxResults('1')
            ->orderBy('s.order', 'ASC');


        $subquery = $this->em->createQueryBuilder();
        $subquery->select('userSignature')
            ->from('HospitalApi\Entity\EletronicDocumentSignature', 'signature')
            ->innerJoin('HospitalApi\Entity\User', 'userSignature', 'WITH', 'userSignature = signature.user')
            //->where('signature.signed = 0')
            ->groupBy('signature._document')
            ->orderBy('signature.order', 'ASC');

        $select = $this->em->createQueryBuilder();
        $select->select('ed')
            ->from($this->getEntityPath(), 'ed')
            ->innerJoin("HospitalApi\Entity\User", "u", "with", "ed.user = u")
            ->leftJoin("HospitalApi\Entity\EletronicDocumentSignature", 'eds', 'WITH', 'eds._document = ed')
            ->leftJoin("eds.user", 'us', 'WITH', 'u = :user OR us = :user')
            ->where( $select->expr()->eq( 'us', $select->expr()->any( $subquery->getDQL() )) )
            //   ->Need a andWhere here<-
            ->orwhere('u = :user')
            ->setParameter( 'user', $this->getSession() )
            ->andWhere('ed.c_removed = 0');
        return $select;
    }

我想知道如何将andWhere()子句添加到执行以下操作的$ select查询中:

CASE WHEN 'ed.user' = ':user' THEN $nextToSign = ':user'

我尝试过

->andWhere( 'CASE WHEN', $select->expr()->neq('ed.user', ':user'), 'THEN', '$nextToSign = :user' )
->andWhere( 'CASE', 'WHEN', $select->expr()->neq('ed.user', ':user'), 'THEN', $nextToSign = ':user' )
->andWhere( 'CASE', 'WHEN', $select->expr()->neq('ed.user', ':user'), 'THEN', $select->expr()->eq($nextToSign, ':user') )
->andWhere( "CASE", "WHEN", $select->expr()->neq("ed.user", ":user"), "THEN", $select->expr()->eq(":user", $nextToSign ), "END" )

和其他一些变体

这些都不起作用。是否可以使用查询生成器进行这种查询?

1 个答案:

答案 0 :(得分:1)

您需要在语句末尾添加END

请参考EBNF notation中的case语句以获取确切的语法规范。

所以您的代码可能需要像这样:

->andWhere('CASE WHEN ed.user != :user THEN <expr> END')

我不确定您的$select->expr()->eq($nextToSign, ':user')语句实际上意味着什么,请记住您的代码示例表明$nextToSign是查询生成器。因此,我用<expr>代替了您的陈述的这一部分。