Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.5] Configurability of local key when using a BelongsToMany relationship #17903

Merged
merged 3 commits into from
Feb 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public function morphMany($related, $name, $type = null, $id = null, $localKey =
* @param string $relation
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function belongsToMany($related, $table = null, $foreignKey = null, $relatedKey = null, $relation = null)
public function belongsToMany($related, $table = null, $foreignKey = null, $relatedKey = null, $localKey = null, $relation = null)
{
// If no relationship name was passed, we will pull backtraces to get the
// name of the calling function. We will use that function name as the
Expand All @@ -307,6 +307,8 @@ public function belongsToMany($related, $table = null, $foreignKey = null, $rela

$relatedKey = $relatedKey ?: $instance->getForeignKey();

$localKey = $localKey ?: $this->getKeyName();

// If no table name was provided, we can guess it by concatenating the two
// models using underscores in alphabetical order. The two model names
// are transformed to snake case from their default CamelCase also.
Expand All @@ -315,7 +317,7 @@ public function belongsToMany($related, $table = null, $foreignKey = null, $rela
}

return new BelongsToMany(
$instance->newQuery(), $this, $table, $foreignKey, $relatedKey, $relation
$instance->newQuery(), $this, $table, $foreignKey, $relatedKey, $localKey, $relation
);
}

Expand Down
5 changes: 3 additions & 2 deletions src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,13 @@ class BelongsToMany extends Relation
* @param string $relationName
* @return void
*/
public function __construct(Builder $query, Model $parent, $table, $foreignKey, $relatedKey, $relationName = null)
public function __construct(Builder $query, Model $parent, $table, $foreignKey, $relatedKey, $localKey, $relationName = null)
{
$this->table = $table;
$this->relatedKey = $relatedKey;
$this->foreignKey = $foreignKey;
$this->relationName = $relationName;
$this->localKey = $localKey;

parent::__construct($query, $parent);
}
Expand Down Expand Up @@ -140,7 +141,7 @@ protected function performJoin($query = null)
// model instance. Then we can set the "where" for the parent models.
$baseTable = $this->related->getTable();

$key = $baseTable.'.'.$this->related->getKeyName();
$key = $baseTable.'.'.$this->localKey;

$query->join($this->table, $key, '=', $this->getQualifiedRelatedKeyName());

Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Eloquent/Relations/MorphToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ class MorphToMany extends BelongsToMany
* @param bool $inverse
* @return void
*/
public function __construct(Builder $query, Model $parent, $name, $table, $foreignKey, $relatedKey, $relationName = null, $inverse = false)
public function __construct(Builder $query, Model $parent, $name, $table, $foreignKey, $relatedKey, $localKey, $relationName = null, $inverse = false)
{
$this->inverse = $inverse;
$this->morphType = $name.'_type';
$this->morphClass = $inverse ? $query->getModel()->getMorphClass() : $parent->getMorphClass();

parent::__construct($query, $parent, $table, $foreignKey, $relatedKey, $relationName);
parent::__construct($query, $parent, $table, $foreignKey, $relatedKey, $localKey, $relationName);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/Database/DatabaseEloquentBelongsToManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ public function getRelation()
{
list($builder, $parent) = $this->getRelationArguments();

return new BelongsToMany($builder, $parent, 'user_role', 'user_id', 'role_id', 'relation_name');
return new BelongsToMany($builder, $parent, 'user_role', 'user_id', 'role_id', 'id', 'relation_name');
}

public function getRelationArguments()
Expand All @@ -728,7 +728,7 @@ public function getRelationArguments()
$builder->shouldReceive('join')->once()->with('user_role', 'roles.id', '=', 'user_role.role_id');
$builder->shouldReceive('where')->once()->with('user_role.user_id', '=', 1);

return [$builder, $parent, 'user_role', 'user_id', 'role_id', 'relation_name'];
return [$builder, $parent, 'user_role', 'user_id', 'role_id', 'id', 'relation_name'];
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Database/DatabaseEloquentMorphToManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function getRelation()
{
list($builder, $parent) = $this->getRelationArguments();

return new MorphToMany($builder, $parent, 'taggable', 'taggables', 'taggable_id', 'tag_id');
return new MorphToMany($builder, $parent, 'taggable', 'taggables', 'taggable_id', 'tag_id', 'id');
}

public function getRelationArguments()
Expand All @@ -98,7 +98,7 @@ public function getRelationArguments()
$builder->shouldReceive('where')->once()->with('taggables.taggable_id', '=', 1);
$builder->shouldReceive('where')->once()->with('taggables.taggable_type', get_class($parent));

return [$builder, $parent, 'taggable', 'taggables', 'taggable_id', 'tag_id', 'relation_name', false];
return [$builder, $parent, 'taggable', 'taggables', 'taggable_id', 'tag_id', 'id', 'relation_name', false];
}
}

Expand Down