Code snippets for symfony 1.x

Navigation

Refine Tags

Snippets tagged "query doctrine grouping"

Doctrine custom grouping

This is how to create a complex grouping using doctrine query.

sample sql statement

    select ... from ... where x = 1 and (a in(1,2,3) or a is not null)
 

Extend Doctrine_Query

    class myDQ extends Doctrine_Query
    {
      public static function create($conn = null) {
        return new myDQ($conn);
      }
 
      public function toGroup()
      {
        $where = $this->_dqlParts['where'];
        if (count($where) > 0) {
          array_splice($where, count($where) - 1, 0, '(');
          $this->_dqlParts['where'] = $where;
        }
 
        return $this;
 
      }
 
      public function endGroup()
      {
        $where = $this->_dqlParts['where'];
        if (count($where) > 0) 
        {
          $where[] = ')';
          $this->_dqlParts['where'] = $where;
        }  
 
        return $this;
      }
 
    }
 

Usage

    $q = myDQ::create()
      ->from('Some s')
      ->where('s.x = ?', 1)
      ->andWhereIn('s.a', array(1,2,3))
      ->toGroup()
      ->orWhere('s.a is not null')
      ->endGroup();
 
by ken marfilla on 2009-11-11, tagged doctrine  grouping  parenthesis  query