/** * Creates INNER JOIN SQL for associations. * * @param Table $from_table the table used for the FROM SQL statement * @param bool $using_through is this a THROUGH relationship? * @param string $alias a table alias for when a table is being joined twice * @return string SQL INNER JOIN fragment */ public function constructInnerJoinSql(Table $from_table, $using_through = false, $alias = null) { if ($using_through) { $join_table = $from_table; $join_table_name = $from_table->getFullyQualifiedTableName(); $from_table_name = Table::load($this->class_name)->getFullyQualifiedTableName(); } else { $join_table = Table::load($this->class_name); $join_table_name = $join_table->getFullyQualifiedTableName(); $from_table_name = $from_table->getFullyQualifiedTableName(); } // need to flip the logic when the key is on the other table if ($this instanceof HasMany || $this instanceof HasOne) { $this->setKeys($from_table->class->getName()); if ($using_through) { $foreign_key = $this->primary_key[0]; $join_primary_key = $this->foreign_key[0]; } else { $join_primary_key = $this->foreign_key[0]; $foreign_key = $this->primary_key[0]; } } else { $foreign_key = $this->foreign_key[0]; $join_primary_key = $this->primary_key[0]; } if (!\is_null($alias)) { $aliased_join_table_name = $alias = $this->getTable()->conn->quoteName($alias); $alias .= ' '; } else { $aliased_join_table_name = $join_table_name; } return "INNER JOIN {$join_table_name} {$alias}ON({$from_table_name}.{$foreign_key} = {$aliased_join_table_name}.{$join_primary_key})"; }