Php 论质疑中的括号

Php 论质疑中的括号,php,doctrine,Php,Doctrine,嗨 我必须嵌套一些或/和条件 但我需要在sql语句中使用括号才能按正确的顺序执行 但是你是怎么做到的 应该是这种形式 (……或……)和 根据这篇博文,“,你需要做一个$query->where(“(条件a或条件b)和条件c”) 这可能看起来像: Doctrine_Query::create() ->from(...) ->where('A = ? OR B = ?', array(valA, valB)) ->andWhere('C = ?', valC

嗨 我必须嵌套一些或/和条件 但我需要在sql语句中使用括号才能按正确的顺序执行 但是你是怎么做到的

应该是这种形式 (……或……)和


根据这篇博文,“,你需要做一个
$query->where(“(条件a或条件b)和条件c”)

这可能看起来像:

Doctrine_Query::create()
    ->from(...)
    ->where('A = ? OR B = ?', array(valA, valB))
    ->andWhere('C = ?', valC);
然而,该海报确实提供了一个更通用的解决方案,
whereParenWrap()
,它扩展了
Doctrine\u Query

DQ::create()
->from(...)
->where('A = ?', valA)
->orWhere('B = ?', valB)
->whereParenWrap()
->andWhere('C = ?', valC);
答案的附件: 稍加修改。 摘自:

现在您可以使用Expr::orX()


显然博客海报是。
/*
 * This is a simple short-hand wrapper for Doctrine_Query. It provides
 * a shorter class name and a few additional functions.
 */
class DQ extends Doctrine_Query
{
    /**
     * Returns a DQ object to get started
     *
     * @return DQ
     */
    public static function create($conn = null, $class = null) {
        return new DQ($conn, $class);
    }

    /**
     * This function will wrap the current dql where statement
     * in parenthesis. This allows more complex dql statements
     * It can be called multiple times during the creation of the dql
     * where clause.
     *
     * @return $this
     */
    public function whereParenWrap() {
        $where = $this->_dqlParts['where'];
        if (count($where) > 0) {
            array_unshift($where, '(');
            array_push($where, ')');
            $this->_dqlParts['where'] = $where;
        }

        return $this;
    }
}

?>
        /** To combine all the OR conditions in one parenthesis, we will collect the conditions in one array */
        $orX = [];
        foreach ($dto->userNameSearchPhrases as $key => $userNameSearchPhrase) {
            $orX[] = $qb->expr()->like('us.username', ':userNameSearchPhrase' . $key);
            $qb->setParameter('userNameSearchPhrase' . $key, $userNameSearchPhrase);
        }
        /** To pass parameters to the Expr::orX() method from an array, use ReflectionMethod */
        $reflectionMethod = new \ReflectionMethod(\Doctrine\ORM\Query\Expr::class, 'orX');
        $orXObject = $reflectionMethod->invokeArgs($qb->expr(), $orX);
        $qb->andWhere($orXObject);