Symfony如何在'orx'中使用'and'/'andx'

xlpyo6sf  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(123)

在Symfony 2中构建复杂查询有更好的方法吗?我的真实的查询相当复杂,但可以简化为“A和((B和C)或(B和D))”(我知道数学方程是“A和B和(C或D)",但我的真实的查询不能简化).我有使用andWhere和orX的经验,但我的问题是如何在'orX'中使用'and' / 'expr()->andX'。
下面的例子(问题是关于orX内部的伪代码部分):

$qBuilder = $repo->createQueryBuilder('s')
->select('s.id')
->Where('s.FirstName = :fname')
->Where('s.LastName = :lname')
->andWhere($qBuilder->expr()->orX(
    (':email is not empty AND s.Email = :email'),
    (':phone is not empty AND s.HomePhone = :phone'),
    (':phone is not empty AND s.StudentMobile = :phone'),
    (':mphone is not empty AND s.HomePhone = :mphone'),
    (':mphone is not empty AND s.StudentMobile = :mphone')
    ))
->setParameter('fname', strtolower($fname))
->setParameter('lname', strtolower($lname))
->setParameter('email', $email)
->setParameter('phone', $phoneNumber)
->setParameter('mphone', $studentmobile);

字符串

toe95027

toe950271#

只需在内部创建一个andX() expr。

->andWhere($qb->expr()->orX(
    $qb->expr()->andX(':email is not empty', 's.Email = :email'),
    $qb->expr()->andX(':phone is not empty', 's.HomePhone = :phone'),
    $qb->expr()->andX(':phone is not empty', 's.StudentMobile = :phone'),
    $qb->expr()->andX(':mphone is not empty', 's.HomePhone = :mphone'),
    $qb->expr()->andX(':mphone is not empty', 's.StudentMobile = :mphone')
))

字符串

相关问题