public function construct_inner_join_sql(Table $from_table, $using_through = false, $alias = null)
{
if ($using_through) {
$join_table = $from_table;
$join_table_name = $from_table->get_fully_qualified_table_name();
$from_table_name = Table::load($this->class_name)->get_fully_qualified_table_name();
} else {
$join_table = Table::load($this->class_name);
$join_table_name = $join_table->get_fully_qualified_table_name();
$from_table_name = $from_table->get_fully_qualified_table_name();
}
// need to flip the logic when the key is on the other table
if ($this instanceof HasMany || $this instanceof HasOne) {
$this->set_keys($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->get_table()->conn->quote_name($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})";
}