/**
  * Build and add an oder field to the query builder.
  * 
  * @param \Doctrine\Orm\QueryBuilder $qb
  * @param mixed $order
  * @return Gmg_Service_Abstract
  */
 protected function addOrder($qb, $order)
 {
     // add the where expression to the query
     // process the $order
     if (is_string($order)) {
         // straight DQL string
         $qb->orderBy($order);
     } elseif (is_array($order) && count($order)) {
         // loop through each order clause supplied
         foreach ($order as $col => $dir) {
             // set the alias to the default
             $alias = $this->alias;
             // if col relates to Relation i.e. Role.id
             // then set up the alias and column names (the join should have
             // already been performed in the $where)
             // TODO: this will cause an error if the column wasn't specified in the $where
             if (strpos($col, '.') !== false) {
                 $parts = explode('.', $col);
                 $col = array_pop($parts);
                 $par = $this->alias;
                 // test for existing joins
                 $as = array();
                 foreach ($qb->getDQLPart('join') as $j) {
                     $as[] = $j;
                 }
                 foreach ($parts as $rel) {
                     $alias = strtolower($rel);
                     $jt = new Expr\Join(Expr\Join::LEFT_JOIN, $par . '.' . $rel, $alias);
                     if (!strpos($qb->getDql(), $jt->__toString()) !== false) {
                         $qb->leftJoin($par . '.' . $rel, $alias);
                     }
                     $par = $alias;
                 }
             }
             $qb->addOrderBy($alias . '.' . $col, $dir);
         }
     }
     return $this;
 }