Join Option

@param  string $table
@param  string $foreignKey1
@param  string $foreignKey2
@param  string $relation
@return boolean
553510
public function join($table, $foreignKey1, $operator, $foreignKey2, $relation = "INNER")
    {
        $self               = self::getInstance();
        $self->optionJoin[] = " {$relation} JOIN {$table} ON {$foreignKey1}{$operator}{$foreignKey2}";
        return $self;
    }
288848
public function join($table, $field1, $operator, $field2) {
        array_push($this->_joins, ' INNER JOIN ' . $table . ' ON ' . $field1 . ' ' . $operator . ' ' . $field2 . ' ');
    
        return $this;
    }
1080589
public function setRelation($relation, $relationColumn = null)
    {
        $this->relation = $relation;
        $this->relationColumn = $relationColumn;

        return $this;
    }
1119861
function DropForeignKey($table, $foreignKey)
    {
        $escaper = $this->GetEscaper();
        $query = 'ALTER TABLE ' . $escaper->EscapeIdentifier($table) . 
                ' DROP FOREIGN KEY ' . $escaper->EscapeIdentifier($foreignKey);
        $this->ExecuteQuery($query);
    }
788459
public function join($table, $constraint, $tableAlias = null)
    {
        return $this->addJoinSource('', $table, $constraint, $tableAlias);
    }
384333
public function join($linkTableName, $relation, $fields = '*', $where = [], $orderBy = ''): Row
    {
        // 生成关联表模型对象
        $table = $this->makeTable($linkTableName);

        // 生成本表关联字段与关联表关联字段
        list ($field, $linkField) = $this->relation($relation);

        // 如果本表关联字段不存在
        if (!array_key_exists($field, $this->data)) {
            return $this;
        }

        // 生成本表关联字段的所有值
        $key = $this->data[$field];

        // 生成关联表查询条件
        $where[$linkField] = $key;

        // 关联表查询结果
        $linkResult = $table->select($this->append($fields, $linkField), $where, $orderBy);

        $this->data[$table->alias()] = $linkResult;

        return $this;
    }
1231556
public function join(string $table, string $one, string $operator = '=', string $two = ''): BaseQueryRelateContract
    {
        $this->query->join($table, $one, $operator, $two);

        return $this;
    }
983121
public static function relation($local, $foreign)
    {
        $relations = Config::table($local)->relations();
        if (isset($relations->{$foreign}))
        {
            return true;
        }

        throw new dbException('Relation "' . $local . '" to "' . $foreign . '" doesn\'t exist');
    }
72453
public function belongsTo($entity, string $related, string $foreignKey = null, string $otherKey = null): BelongsTo
    {
        // Add the relation to the definition in map
        list(, $caller) = debug_backtrace(false);
        $relation = $caller['function'];
        $this->relatedClasses[$relation] = $related;

        $this->addSingleRelation($relation);
        $this->addLocalRelation($relation);

        // If no foreign key was supplied, we can use a backtrace to guess the proper
        // foreign key name by using the name of the relationship function, which
        // when combined with an "_id" should conventionally match the columns.
        if (is_null($foreignKey)) {
            $foreignKey = snake_case($relation).'_id';
        }

        $this->localForeignKeys[$relation] = $foreignKey;

        $relatedMapper = Manager::getInstance()->mapper($related);

        $otherKey = $otherKey ?: $relatedMapper->getEntityMap()->getKeyName();

        return new BelongsTo($relatedMapper, $entity, $foreignKey, $otherKey, $relation);
    }
375217
public function leftJoin($table, $column1 = null, $operator = null, $column2 = null)
	{
		return $this->join($table, $column1, $operator, $column2, 'LEFT OUTER');
	}
40353
public function belongsTo( $related, $foreignKey = null, $otherKey = null, $relation = null )
	{
		// If no relation name was given, we will use this debug backtrace to extract
		// the calling method's name and use that as the relationship name as most
		// of the time this will be what we desire to use for the relationships.
		if ( is_null( $relation ) )
		{
			list( $current, $caller ) = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 2 );

			$relation = $caller['function'];
		}

		// If no foreign key was supplied, we can use a backtrace to guess the proper
		// foreign key name by using the name of the relationship function, which
		// when combined with an "_id" should conventionally match the columns.
		if ( is_null( $foreignKey ) )
		{
			$foreignKey = Str::snake( $relation ) . '_id';
		}

		$instance = new $related;

		// Once we have the foreign key names, we'll just create a new Eloquent query
		// for the related models and returns the relationship instance which will
		// actually be responsible for retrieving and hydrating every relations.
		$query = $instance->newQuery();

		$otherKey = $otherKey ?: $instance->getKeyName();

		return new BelongsTo( $query, $this, $foreignKey, $otherKey, $relation );
	}
584409
public function match(array $results, $relation)
    {
        $foreign = $this->foreignKey;

        $other = $this->otherKey;

        // Execute the relationship and get related entities as an EntityCollection
        $entities = $this->getEager();

        // First we will get to build a dictionary of the child models by their primary
        // key of the relationship, then we can easily match the children back onto
        // the parents using that dictionary and the primary key of the children.
        $dictionary = [];

        // TODO ; see if otherKey is the primary key of the related entity, we can
        // simply use the EntityCollection key to match entities to results, which
        // will be much more efficient, and use this method as a fallback if the
        // otherKey is not the same as the primary Key.
        foreach ($entities as $entity) {
            $entity = $this->factory->make($entity);
            $dictionary[$entity->getEntityAttribute($other)] = $entity->getObject();
        }

        // Once we have the dictionary constructed, we can loop through all the parents
        // and match back onto their children using these keys of the dictionary and
        // the primary key of the children to map them onto the correct instances.
        return array_map(function ($result) use ($dictionary, $foreign, $relation) {
            if (array_key_exists($foreign, $result) && isset($dictionary[$result[$foreign]])) {
                $result[$relation] = $dictionary[$result[$foreign]];
            } else {
                $result[$relation] = null;
            }

            return $result;
        }, $results);
    }
307559
public function hasOne(string $related, string $primary_key = '', string $foreign_key = '')
    {
        $relatedModel = app()->make($related);
        $local_key = $local_key ?? $this->getKey();
        // Make the table name singular and append ID to it
        // FIXME: Use a more reliable approach
        $foreign_key = $foreign_key ?? rtrim($relatedModel->getTable(), 's').'_id';
        return new HasOne($relatedModel, $this, $primary_key, $foreign_key);
    }
1148850
public static function add_foreign_key($table, $foreign_key)
	{
		if ( ! is_array($foreign_key))
		{
			throw new InvalidArgumentException('Foreign key for add_foreign_key() must be specified as an array');
		}

		$sql = 'ALTER TABLE ';
		$sql .= \DB::quote_identifier(\DB::table_prefix($table, static::$connection)).' ';
		$sql .= 'ADD ';
		$sql .= ltrim(static::process_foreign_keys(array($foreign_key)), ',');

		return \DB::query($sql, \DB::UPDATE)->execute();
	}
1374280
public function on($foreignKey, $key = null) {
        if (is_array($foreignKey)) {
            $this->_conditions = array_replace($this->_conditions, $foreignKey);
        } else {
            $this->_conditions[$foreignKey] = $key;
        }

        return $this;
    }
228104
protected function isRelationTable($table)
	{
		$pk=$table->primaryKey;
        $count=is_array($pk) ? count($pk) : 1;
        return ($count === 2 // we want 2 columns
			&& isset($table->foreignKeys[$pk[0]]) // pk column 1 is also a foreign key
			&& isset($table->foreignKeys[$pk[1]]) // pk column 2 is also a foriegn key
			&& $table->foreignKeys[$pk[0]][0] !== $table->foreignKeys[$pk[1]][0]); // and the foreign keys point different tables
	}
639901
public function join($type, $table, array $relations = array())
    {
        $this->append(sprintf(' %s JOIN %s ', $type, $table));

        // Append relations if provided
        if (!empty($relations)) {
            // First on
            $this->on();

            $i = 0; // Iteration counter
            $count = count($relations);

            foreach ($relations as $column => $value) {
                // Increment iteration count
                $i++;

                $this->equals($column, $value);

                // If last iteration
                if ($i != $count) {
                    $this->rawAnd();
                }
            }
        }

        return $this;
    }