Skip to content

Commit

Permalink
Multiple changes to improve support for relationship pivot data
Browse files Browse the repository at this point in the history
Pivot::fromAttributes() instead of new Pivot(): laravel/framework@06f01cf & laravel/framework@4b30aad

(protected) Pivot()->parent -> (public) Pivot()->pivotParent: laravel/framework@29a181f & laravel/framework@22c3c02

$otherKey -> $relatedKey: laravel/framework@d2a7777

Added fromRawAttributes() method to Pivot: laravel/framework@f356419

Various improvements to setKeysForSaveQuery(): laravel/framework@8d82618, laravel/framework@e9f37ed

Fire model events when deleting Pivot models: laravel/framework#27571

Fixed Pivot serialization: laravel/framework@b52d314 & laravel/framework#31956
  • Loading branch information
LukeTowers committed Jan 24, 2022
1 parent a208f57 commit 3e8660e
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 61 deletions.
37 changes: 24 additions & 13 deletions src/Database/Concerns/HasRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,18 @@ 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 (array_key_exists('otherKey', $options) && !array_key_exists('relatedKey', $options)) {
$definitions[$relation]['relatedKey'] = $options['otherKey'];
}
}

return $definitions;
}

return [];
Expand Down Expand Up @@ -309,18 +320,18 @@ 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', 'relatedKey', 'parentKey', 'relatedKey', 'pivot', 'timestamps']);
$relationObj = $this->$relationType($relation[0], $relation['table'], $relation['key'], $relation['relatedKey'], $relation['parentKey'], $relation['relatedKey'], $relationName);
break;

case 'morphTo':
Expand All @@ -335,13 +346,13 @@ 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', 'relatedKey', 'parentKey', 'relatedKey', 'pivot', 'timestamps'], ['name']);
$relationObj = $this->$relationType($relation[0], $relation['name'], $relation['table'], $relation['key'], $relation['relatedKey'], $relation['parentKey'], $relation['relatedKey'], false, $relationName);
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', 'relatedKey', 'parentKey', 'relatedKey', 'pivot', 'timestamps'], ['name']);
$relationObj = $this->$relationType($relation[0], $relation['name'], $relation['table'], $relation['key'], $relation['relatedKey'], $relation['parentKey'], $relation['relatedKey'], $relationName);
break;

case 'attachOne':
Expand All @@ -352,8 +363,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

0 comments on commit 3e8660e

Please sign in to comment.