【问题标题】:Symfony createQueryBuilder, how use 'and'/'andx' inside 'orx'Symfony createQueryBuilder,如何在 'orx' 中使用 'and'/'andx'
【发布时间】:2015-11-06 17:49:20
【问题描述】:

有没有更好的方法在 Symfony 2 中构建复杂的查询? 我的真实查询非常复杂,但可以简化为“A and ((B and C) or (B and D))”(我知道数学方程为“A and B and (C or 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);

【问题讨论】:

    标签: php symfony expr


    【解决方案1】:

    只需在里面创建一个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')
    ))
    

    【讨论】:

    • 另一个问题(我也开始一个新问题)。当值为 '' 或其他值时,为什么我总是从 $expr->neq('s.studentEmail', '') 得到 true?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-01
    • 1970-01-01
    • 2023-03-10
    相关资源
    最近更新 更多