Brackets in a Doctrine request

hi i need to enclose some or / or conditions but i need brackets in my sql statement to do it in the correct order but how do you do it?

must be in this form (... OR ...) And ...

Thnx

+3
source share
2 answers

According to this blog post, “Solving the Problem of the Holographic Doctrine, ” you need to do$query->where("(ConditionA OR ConditionB) AND ConditionC");

It might look like this:

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

However, the poster provides a more general solution whereParenWrap(), expanding Doctrine_Query:

DQ::create()
->from(...)
->where('A = ?', valA)
->orWhere('B = ?', valB)
->whereParenWrap()
->andWhere('C = ?', valC);
+4
source

Add-on for the answer: Brackets in the Doctrine request  

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

0

Source: https://habr.com/ru/post/1781244/


All Articles