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] Apply custom pivot model attribute casting on arrays #21275

Merged
merged 2 commits into from
Sep 20, 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 @@ -188,7 +188,9 @@ public function updateExistingPivot($id, array $attributes, $touch = true)
$attributes = $this->addTimestampsToAttachment($attributes, true);
}

$updated = $this->newPivotStatementForId($id)->update($attributes);
$updated = $this->newPivotStatementForId($id)->update(
$this->castAttributes($attributes)
);

if ($touch) {
$this->touchIfTouching();
Expand Down Expand Up @@ -236,10 +238,6 @@ protected function formatAttachRecords($ids, array $attributes)
// To create the attachment records, we will simply spin through the IDs given
// and create a new record to insert for each ID. Each ID may actually be a
// key in the array, with extra attributes to be placed in other columns.
$attributes = $this->using
? $this->newPivot()->forceFill($attributes)->getAttributes()
: $attributes;

foreach ($ids as $key => $value) {
$records[] = $this->formatAttachRecord(
$key, $value, $attributes, $hasTimestamps
Expand All @@ -263,7 +261,7 @@ protected function formatAttachRecord($key, $value, $attributes, $hasTimestamps)
list($id, $attributes) = $this->extractAttachIdAndAttributes($key, $value, $attributes);

return array_merge(
$this->baseAttachRecord($id, $hasTimestamps), $attributes
$this->baseAttachRecord($id, $hasTimestamps), $this->castAttributes($attributes)
);
}

Expand Down Expand Up @@ -503,4 +501,17 @@ protected function castKey($key)
{
return is_numeric($key) ? (int) $key : (string) $key;
}

/**
* Cast the given pivot attributes.
*
* @param array $attributes
* @return array
*/
protected function castAttributes($attributes)
{
return $this->using
? $this->newPivot()->forceFill($attributes)->getAttributes()
: $attributes;
}
}
78 changes: 78 additions & 0 deletions tests/Integration/Database/EloquentCustomPivotCastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,30 @@ public function test_casts_are_respected_on_attach()
$this->assertEquals(['foo' => 'bar'], $project->collaborators[0]->pivot->permissions);
}

public function test_casts_are_respected_on_attach_array()
{
$user = CustomPivotCastTestUser::forceCreate([
'email' => '[email protected]',
]);

$user2 = CustomPivotCastTestUser::forceCreate([
'email' => '[email protected]',
]);

$project = CustomPivotCastTestProject::forceCreate([
'name' => 'Test Project',
]);

$project->collaborators()->attach([
$user->id => ['permissions' => ['foo' => 'bar']],
$user2->id => ['permissions' => ['baz' => 'bar']],
]);
$project = $project->fresh();

$this->assertEquals(['foo' => 'bar'], $project->collaborators[0]->pivot->permissions);
$this->assertEquals(['baz' => 'bar'], $project->collaborators[1]->pivot->permissions);
}

public function test_casts_are_respected_on_sync()
{
$user = CustomPivotCastTestUser::forceCreate([
Expand All @@ -76,6 +100,60 @@ public function test_casts_are_respected_on_sync()

$this->assertEquals(['foo' => 'bar'], $project->collaborators[0]->pivot->permissions);
}

public function test_casts_are_respected_on_sync_array()
{
$user = CustomPivotCastTestUser::forceCreate([
'email' => '[email protected]',
]);

$user2 = CustomPivotCastTestUser::forceCreate([
'email' => '[email protected]',
]);

$project = CustomPivotCastTestProject::forceCreate([
'name' => 'Test Project',
]);

$project->collaborators()->sync([
$user->id => ['permissions' => ['foo' => 'bar']],
$user2->id => ['permissions' => ['baz' => 'bar']],
]);
$project = $project->fresh();

$this->assertEquals(['foo' => 'bar'], $project->collaborators[0]->pivot->permissions);
$this->assertEquals(['baz' => 'bar'], $project->collaborators[1]->pivot->permissions);
}

public function test_casts_are_respected_on_sync_array_while_updating_existing()
{
$user = CustomPivotCastTestUser::forceCreate([
'email' => '[email protected]',
]);

$user2 = CustomPivotCastTestUser::forceCreate([
'email' => '[email protected]',
]);

$project = CustomPivotCastTestProject::forceCreate([
'name' => 'Test Project',
]);

$project->collaborators()->attach([
$user->id => ['permissions' => ['foo' => 'bar']],
$user2->id => ['permissions' => ['baz' => 'bar']],
]);

$project->collaborators()->sync([
$user->id => ['permissions' => ['foo1' => 'bar1']],
$user2->id => ['permissions' => ['baz2' => 'bar2']],
]);

$project = $project->fresh();

$this->assertEquals(['foo1' => 'bar1'], $project->collaborators[0]->pivot->permissions);
$this->assertEquals(['baz2' => 'bar2'], $project->collaborators[1]->pivot->permissions);
}
}

class CustomPivotCastTestUser extends Model
Expand Down