/** * Creates INNER JOIN SQL for associations. * * @param Table $fromTable the table used for the FROM SQL statement * @param bool $usingThrough 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 $fromTable, $usingThrough = false, $alias = null) { if ($usingThrough) { $joinTable = $fromTable; $joinTableName = $fromTable->getFullyQualifiedTableName(); $fromTableName = Table::load($this->className)->getFullyQualifiedTableName(); } else { $joinTable = Table::load($this->className); $joinTableName = $joinTable->getFullyQualifiedTableName(); $fromTableName = $fromTable->getFullyQualifiedTableName(); } // need to flip the logic when the key is on the other table if ($this instanceof HasMany || $this instanceof HasOne) { $this->setKeys($fromTable->class->getName()); if ($usingThrough) { $foreignKey = $this->primaryKey[0]; $joinPrimaryKey = $this->foreignKey[0]; } else { $joinPrimaryKey = $this->foreignKey[0]; $foreignKey = $this->primaryKey[0]; } } else { $foreignKey = $this->foreignKey[0]; $joinPrimaryKey = $this->primaryKey[0]; } if (!is_null($alias)) { $aliasedJoinTableName = $alias = $this->getTable()->conn->quoteName($alias); $alias .= ' '; } else { $aliasedJoinTableName = $joinTableName; } return "INNER JOIN {$joinTableName} {$alias}ON({$fromTableName}.{$foreignKey} = {$aliasedJoinTableName}.{$joinPrimaryKey})"; }