/**
  * Builds SQL statements for a relationship field map.
  * @param epFieldMap $fm
  * @param string $alias_a the alias of the parent class
  * @return string|array (error message if string)
  */
 protected function _buildSqlFieldMapRelationship($fm, $alias_a = self::DUMMY_ALIAS)
 {
     // get cm_a, base_a, var_a, base_b
     $cm_a = $fm->getClassMap();
     $base_a = $cm_a->getName();
     $var_a = $fm->getName();
     $base_b = $fm->getClass();
     // call manager to get relation table for base class a and b
     if (!($rt = $this->em->getRelationTable($base_a, $base_b))) {
         return "[Internal] No relationship table for [{$base_a}] and [{$base_b}]";
     }
     // change base_a to the actual child being used
     if ($base_a == $this->root_class) {
         $base_a = $this->aliases[$this->root_alias];
     }
     // alias for rt relation table
     $rta = $this->qid($this->generateRtAlias($rt));
     // get class map for base_b
     if (!($cm_base_b = $this->em->getClassMap($base_b))) {
         return "[Internal] No class map for [{$base_b}]";
     }
     // get all subclasses of base_b (true: recursive)
     if (!($cms_b = $cm_base_b->getChildren(true))) {
         // make sure we have an array even if no subclasses
         $cms_b = array();
     }
     // add base_b into array
     $cms_b[] = $cm_base_b;
     // quote value or id (avoid repeating!)
     $alias_a = $this->qid($alias_a);
     $class_a = $this->q($base_a);
     $var_a = $this->q($var_a);
     $oid_a = $this->qid($cm_a->getOidColumn());
     $base_b = $this->q($base_b);
     // array to keep OR conditions
     $wheres = array();
     // go through each subclass
     foreach ($cms_b as $cm_b) {
         // get class_b and generate an alias
         $class_b = $this->q($cm_b->getName());
         $alias_b = $this->qid($alias_b_ = $this->generateAlias($class_b));
         $oid_b = $this->qid($cm_b->getOidColumn());
         // array to keep all AND where conditions
         $where = array();
         $where[] = $rta . '.class_a = ' . $class_a;
         $where[] = $rta . '.var_a = ' . $var_a;
         $where[] = $rta . '.oid_a = ' . $alias_a . '.' . $oid_a;
         $where[] = $rta . '.base_b = ' . $base_b;
         $where[] = $rta . '.class_b = ' . $class_b;
         $where[] = $rta . '.oid_b = ' . $alias_b . '.' . $oid_b;
         // put into AND array
         $wheres[$alias_b_] = implode(' AND ', $where);
         // $alias_b_: alias_b before quoting
     }
     return $wheres;
 }