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

Multiple changes to improve support for relationship pivot data #65

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
50 changes: 37 additions & 13 deletions src/Database/Concerns/HasRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,22 @@ public function getRelationDefinition($name)
public function getRelationTypeDefinitions($type)
{
if (in_array($type, static::$relationTypes)) {
return $this->{$type};
$definitions = $this->{$type} ?: [];

// Handle renaming otherKey to relatedKey
// @see https://github.com/laravel/framework/commit/d2a77776295cb155b985526c1fa1fddc190adb07
// @since v1.1.8
foreach ($definitions as $relation => $options) {
if (
is_array($options)
&& array_key_exists('otherKey', $options)
&& !array_key_exists('relatedPivotKey', $options)
) {
$definitions[$relation]['relatedPivotKey'] = $options['otherKey'];
}
}

return $definitions;
}

return [];
Expand Down Expand Up @@ -309,18 +324,21 @@ protected function handleRelation($relationName)
switch ($relationType) {
case 'hasOne':
case 'hasMany':
$relation = $this->validateRelationArgs($relationName, ['key', 'otherKey']);
$relationObj = $this->$relationType($relation[0], $relation['key'], $relation['otherKey'], $relationName);
$relation = $this->validateRelationArgs($relationName, ['key', 'relatedKey']);
$relationObj = $this->$relationType($relation[0], $relation['key'], $relation['relatedKey'], $relationName);
break;

case 'belongsTo':
$relation = $this->validateRelationArgs($relationName, ['key', 'otherKey']);
$relationObj = $this->$relationType($relation[0], $relation['key'], $relation['otherKey'], $relationName);
$relation = $this->validateRelationArgs($relationName, ['key', 'relatedKey']);
$relationObj = $this->$relationType($relation[0], $relation['key'], $relation['relatedKey'], $relationName);
break;

case 'belongsToMany':
$relation = $this->validateRelationArgs($relationName, ['table', 'key', 'otherKey', 'parentKey', 'relatedKey', 'pivot', 'timestamps']);
$relationObj = $this->$relationType($relation[0], $relation['table'], $relation['key'], $relation['otherKey'], $relation['parentKey'], $relation['relatedKey'], $relationName);
$relation = $this->validateRelationArgs($relationName, ['table', 'key', 'relatedPivotKey', 'parentKey', 'relatedKey', 'pivot', 'timestamps']);
$relationObj = $this->$relationType($relation[0], $relation['table'], $relation['key'], $relation['relatedPivotKey'], $relation['parentKey'], $relation['relatedKey'], $relationName);
if (!empty($relation['pivotModel'])) {
$relationObj->using($relation['pivotModel']);
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be added to the other pivot model using relations (morphToMany & morphedByMany)

break;

case 'morphTo':
Expand All @@ -335,13 +353,19 @@ protected function handleRelation($relationName)
break;

case 'morphToMany':
$relation = $this->validateRelationArgs($relationName, ['table', 'key', 'otherKey', 'parentKey', 'relatedKey', 'pivot', 'timestamps'], ['name']);
$relationObj = $this->$relationType($relation[0], $relation['name'], $relation['table'], $relation['key'], $relation['otherKey'], $relation['parentKey'], $relation['relatedKey'], false, $relationName);
$relation = $this->validateRelationArgs($relationName, ['table', 'key', 'relatedPivotKey', 'parentKey', 'relatedKey', 'pivot', 'timestamps'], ['name']);
$relationObj = $this->$relationType($relation[0], $relation['name'], $relation['table'], $relation['key'], $relation['relatedPivotKey'], $relation['parentKey'], $relation['relatedKey'], false, $relationName);
if (!empty($relation['pivotModel'])) {
$relationObj->using($relation['pivotModel']);
}
break;

case 'morphedByMany':
$relation = $this->validateRelationArgs($relationName, ['table', 'key', 'otherKey', 'parentKey', 'relatedKey', 'pivot', 'timestamps'], ['name']);
$relationObj = $this->$relationType($relation[0], $relation['name'], $relation['table'], $relation['key'], $relation['otherKey'], $relation['parentKey'], $relation['relatedKey'], $relationName);
$relation = $this->validateRelationArgs($relationName, ['table', 'key', 'relatedPivotKey', 'parentKey', 'relatedKey', 'pivot', 'timestamps'], ['name']);
$relationObj = $this->$relationType($relation[0], $relation['name'], $relation['table'], $relation['key'], $relation['relatedPivotKey'], $relation['parentKey'], $relation['relatedKey'], $relationName);
if (!empty($relation['pivotModel'])) {
$relationObj->using($relation['pivotModel']);
}
break;

case 'attachOne':
Expand All @@ -352,8 +376,8 @@ protected function handleRelation($relationName)

case 'hasOneThrough':
case 'hasManyThrough':
$relation = $this->validateRelationArgs($relationName, ['key', 'throughKey', 'otherKey', 'secondOtherKey'], ['through']);
$relationObj = $this->$relationType($relation[0], $relation['through'], $relation['key'], $relation['throughKey'], $relation['otherKey'], $relation['secondOtherKey']);
$relation = $this->validateRelationArgs($relationName, ['key', 'throughKey', 'relatedKey', 'secondOtherKey'], ['through']);
$relationObj = $this->$relationType($relation[0], $relation['through'], $relation['key'], $relation['throughKey'], $relation['relatedKey'], $relation['secondOtherKey']);
break;

default:
Expand Down
4 changes: 2 additions & 2 deletions src/Database/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ public function newPivot(EloquentModel $parent, array $attributes, $table, $exis
{
return $using
? $using::fromRawAttributes($parent, $attributes, $table, $exists)
: new Pivot($parent, $attributes, $table, $exists);
: Pivot::fromAttributes($parent, $attributes, $table, $exists);
}

/**
Expand All @@ -741,7 +741,7 @@ public function newRelationPivot($relationName, $parent, $attributes, $table, $e

if (!is_null($definition) && array_key_exists('pivotModel', $definition)) {
$pivotModel = $definition['pivotModel'];
return new $pivotModel($parent, $attributes, $table, $exists);
return $pivotModel::fromAttributes($parent, $attributes, $table, $exists);
}
}

Expand Down
Loading