-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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.6] Fix pivot serialization #22786
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5fc67b1
write failing test
taylorotwell 77db049
write failing test for collections
taylorotwell e5eef73
fix pivot serialization
taylorotwell bbc9415
formatting
taylorotwell 1b5a642
get rid of group
taylorotwell 6c6a704
fix morph pivot serialization and restoration
taylorotwell 0706ec7
Apply fixes from StyleCI (#22785)
taylorotwell 91dbd29
add to test
taylorotwell 7ac0e57
Merge branch 'fix-pivot-serialization' of github.com:laravel/framewor…
taylorotwell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -146,6 +146,22 @@ public function newPivot(array $attributes = [], $exists = false) | |
return $pivot; | ||
} | ||
|
||
/** | ||
* Get the pivot columns for the relation. | ||
* | ||
* "pivot_" is prefixed ot each column for easy removal later. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo. "prefixed at", should be? |
||
* | ||
* @return array | ||
*/ | ||
protected function aliasedPivotColumns() | ||
{ | ||
$defaults = [$this->foreignPivotKey, $this->relatedPivotKey, $this->morphType]; | ||
|
||
return collect(array_merge($defaults, $this->pivotColumns))->map(function ($column) { | ||
return $this->table.'.'.$column.' as pivot_'.$column; | ||
})->unique()->all(); | ||
} | ||
|
||
/** | ||
* Get the foreign key "type" name. | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
194 changes: 194 additions & 0 deletions
194
tests/Integration/Database/EloquentPivotSerializationTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Integration\Database; | ||
|
||
use Illuminate\Queue\SerializesModels; | ||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\Pivot; | ||
use Illuminate\Database\Eloquent\Relations\MorphPivot; | ||
use Illuminate\Database\Eloquent\Collection as DatabaseCollection; | ||
|
||
/** | ||
* @group integration | ||
*/ | ||
class EloquentPivotSerializationTest extends DatabaseTestCase | ||
{ | ||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
Schema::create('users', function ($table) { | ||
$table->increments('id'); | ||
$table->string('email'); | ||
$table->timestamps(); | ||
}); | ||
|
||
Schema::create('projects', function ($table) { | ||
$table->increments('id'); | ||
$table->string('name'); | ||
$table->timestamps(); | ||
}); | ||
|
||
Schema::create('project_users', function ($table) { | ||
$table->integer('user_id'); | ||
$table->integer('project_id'); | ||
}); | ||
|
||
Schema::create('tags', function ($table) { | ||
$table->increments('id'); | ||
$table->string('name'); | ||
$table->timestamps(); | ||
}); | ||
|
||
Schema::create('taggables', function ($table) { | ||
$table->integer('tag_id'); | ||
$table->integer('taggable_id'); | ||
$table->string('taggable_type'); | ||
}); | ||
} | ||
|
||
public function test_pivot_can_be_serialized_and_restored() | ||
{ | ||
$user = PivotSerializationTestUser::forceCreate(['email' => 'taylor@laravel.com']); | ||
$project = PivotSerializationTestProject::forceCreate(['name' => 'Test Project']); | ||
$project->collaborators()->attach($user); | ||
|
||
$project = $project->fresh(); | ||
|
||
$class = new PivotSerializationTestClass($project->collaborators->first()->pivot); | ||
$class = unserialize(serialize($class)); | ||
|
||
$this->assertEquals($project->collaborators->first()->pivot->user_id, $class->pivot->user_id); | ||
$this->assertEquals($project->collaborators->first()->pivot->project_id, $class->pivot->project_id); | ||
|
||
$class->pivot->save(); | ||
} | ||
|
||
public function test_morph_pivot_can_be_serialized_and_restored() | ||
{ | ||
$project = PivotSerializationTestProject::forceCreate(['name' => 'Test Project']); | ||
$tag = PivotSerializationTestTag::forceCreate(['name' => 'Test Tag']); | ||
$project->tags()->attach($tag); | ||
|
||
$project = $project->fresh(); | ||
|
||
$class = new PivotSerializationTestClass($project->tags->first()->pivot); | ||
$class = unserialize(serialize($class)); | ||
|
||
$this->assertEquals($project->tags->first()->pivot->tag_id, $class->pivot->tag_id); | ||
$this->assertEquals($project->tags->first()->pivot->taggable_id, $class->pivot->taggable_id); | ||
$this->assertEquals($project->tags->first()->pivot->taggable_type, $class->pivot->taggable_type); | ||
|
||
$class->pivot->save(); | ||
} | ||
|
||
public function test_collection_of_pivots_can_be_serialized_and_restored() | ||
{ | ||
$user = PivotSerializationTestUser::forceCreate(['email' => 'taylor@laravel.com']); | ||
$user2 = PivotSerializationTestUser::forceCreate(['email' => 'mohamed@laravel.com']); | ||
$project = PivotSerializationTestProject::forceCreate(['name' => 'Test Project']); | ||
|
||
$project->collaborators()->attach($user); | ||
$project->collaborators()->attach($user2); | ||
|
||
$project = $project->fresh(); | ||
|
||
$class = new PivotSerializationTestCollectionClass(DatabaseCollection::make($project->collaborators->map->pivot)); | ||
$class = unserialize(serialize($class)); | ||
|
||
$this->assertEquals($project->collaborators[0]->pivot->user_id, $class->pivots[0]->user_id); | ||
$this->assertEquals($project->collaborators[1]->pivot->project_id, $class->pivots[1]->project_id); | ||
} | ||
|
||
public function test_collection_of_morph_pivots_can_be_serialized_and_restored() | ||
{ | ||
$tag = PivotSerializationTestTag::forceCreate(['name' => 'Test Tag 1']); | ||
$tag2 = PivotSerializationTestTag::forceCreate(['name' => 'Test Tag 2']); | ||
$project = PivotSerializationTestProject::forceCreate(['name' => 'Test Project']); | ||
|
||
$project->tags()->attach($tag); | ||
$project->tags()->attach($tag2); | ||
|
||
$project = $project->fresh(); | ||
|
||
$class = new PivotSerializationTestCollectionClass(DatabaseCollection::make($project->tags->map->pivot)); | ||
$class = unserialize(serialize($class)); | ||
|
||
$this->assertEquals($project->tags[0]->pivot->tag_id, $class->pivots[0]->tag_id); | ||
$this->assertEquals($project->tags[0]->pivot->taggable_id, $class->pivots[0]->taggable_id); | ||
$this->assertEquals($project->tags[0]->pivot->taggable_type, $class->pivots[0]->taggable_type); | ||
|
||
$this->assertEquals($project->tags[1]->pivot->tag_id, $class->pivots[1]->tag_id); | ||
$this->assertEquals($project->tags[1]->pivot->taggable_id, $class->pivots[1]->taggable_id); | ||
$this->assertEquals($project->tags[1]->pivot->taggable_type, $class->pivots[1]->taggable_type); | ||
} | ||
} | ||
|
||
class PivotSerializationTestClass | ||
{ | ||
use SerializesModels; | ||
|
||
public $pivot; | ||
|
||
public function __construct($pivot) | ||
{ | ||
$this->pivot = $pivot; | ||
} | ||
} | ||
|
||
class PivotSerializationTestCollectionClass | ||
{ | ||
use SerializesModels; | ||
|
||
public $pivots; | ||
|
||
public function __construct($pivots) | ||
{ | ||
$this->pivots = $pivots; | ||
} | ||
} | ||
|
||
class PivotSerializationTestUser extends Model | ||
{ | ||
public $table = 'users'; | ||
} | ||
|
||
class PivotSerializationTestProject extends Model | ||
{ | ||
public $table = 'projects'; | ||
|
||
public function collaborators() | ||
{ | ||
return $this->belongsToMany( | ||
PivotSerializationTestUser::class, 'project_users', 'project_id', 'user_id' | ||
)->using(PivotSerializationTestCollaborator::class); | ||
} | ||
|
||
public function tags() | ||
{ | ||
return $this->morphToMany(PivotSerializationTestTag::class, 'taggable', 'taggables', 'taggable_id', 'tag_id') | ||
->using(PivotSerializationTestTagAttachment::class); | ||
} | ||
} | ||
|
||
class PivotSerializationTestTag extends Model | ||
{ | ||
public $table = 'tags'; | ||
|
||
public function projects() | ||
{ | ||
return $this->morphedByMany(PivotSerializationTestProject::class, 'taggable', 'taggables', 'tag_id', 'taggable_id') | ||
->using(PivotSerializationTestTagAttachment::class); | ||
} | ||
} | ||
|
||
class PivotSerializationTestCollaborator extends Pivot | ||
{ | ||
public $table = 'project_users'; | ||
} | ||
|
||
class PivotSerializationTestTagAttachment extends MorphPivot | ||
{ | ||
public $table = 'taggables'; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about adding
getQueueableId()
to theModel
base class, and overriding it inPivot
? Then you can always map togetQueueableId()
and get rid of theinstanceof
. 😉