/** * {@inheritdoc} */ public function addJoin($type, $table, $alias = NULL, $condition = NULL, $arguments = array()) { if ($table instanceof SelectInterface) { // @todo implement this. throw new \Exception('Subqueries are not supported at this moment.'); } $alias = parent::addJoin($type, $table, $alias, $condition, $arguments); if (isset($type)) { if ($type != 'INNER' && $type != 'LEFT') { throw new \Exception(sprintf('%s type not supported, only INNER and LEFT.', $type)); } if (!preg_match('/^(\\w+\\.)?(\\w+)\\s*=\\s*(\\w+)\\.(\\w+)$/', $condition, $matches)) { throw new \Exception('Only x.field1 = y.field2 conditions are supported.' . $condition); } if (!$matches[1] && count($this->tables) == 2) { $aliases = array_keys($this->tables); $matches[1] = $aliases[0]; } else { $matches[1] = substr($matches[1], 0, -1); } if (!$matches[1]) { throw new \Exception('Only x.field1 = y.field2 conditions are supported.' . $condition); } if ($matches[1] == $alias) { $this->tables[$alias] += array('added_field' => $matches[2], 'original_table_alias' => $matches[3], 'original_field' => $matches[4]); } elseif ($matches[3] == $alias) { $this->tables[$alias] += array('added_field' => $matches[4], 'original_table_alias' => $matches[1], 'original_field' => $matches[2]); } else { throw new \Exception('The JOIN condition does not contain the alias of the joined table.'); } } return $alias; }