Скобки в доктрине запроса - PullRequest
4 голосов
/ 20 декабря 2010

привет я должен вложить некоторые или / и условия но мне нужны скобки в моем SQL-заявлении, чтобы сделать это в правильном порядке но как ты это делаешь

это должно быть в этой форме (... ИЛИ ...) И ...

Thnx

Ответы [ 3 ]

4 голосов
/ 20 декабря 2010

Согласно этому сообщению в блоге " Решение проблемы скобок доктрины ", вам нужно выполнить $query->where("(ConditionA OR ConditionB) AND ConditionC");

, которое может выглядеть следующим образом:

Doctrine_Query::create()
    ->from(...)
    ->where('A = ? OR B = ?', array(valA, valB))
    ->andWhere('C = ?', valC);

Однако плакат предоставляет более общее решение, whereParenWrap(), путем расширения Doctrine_Query:

DQ::create()
->from(...)
->where('A = ?', valA)
->orWhere('B = ?', valB)
->whereParenWrap()
->andWhere('C = ?', valC);
0 голосов
/ 21 августа 2018

Теперь вы можете использовать Expr :: orX ()

        /** 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);
0 голосов
/ 13 июня 2011

Аддон для ответа: Скобки в запросе Doctrine

/*
 * 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;
    }
}

?>

Слегка изменено.Взято из: https://gist.github.com/888386/634c51993aaf16565690be10da7b8f13a227020a

...