Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit

Permalink
Merge pull request #5641 from Laravel-Backpack/fix-belongsToMany-with…
Browse files Browse the repository at this point in the history
…-subfields-dynamic-relation

fix belongsToMany with subfields dynamic relation
  • Loading branch information
pxpm authored Aug 29, 2024
2 parents 61939b9 + de1a6a4 commit e38cc68
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/app/Library/CrudPanel/Traits/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private function createRelationsForItem($item, $formattedRelations)
}
foreach ($values as $value) {
// if it's an existing pivot, update it
$attributes = $this->preparePivotAttributesForSave($value, $relation, $item->getKey(), $keyName);
$attributes = $this->preparePivotAttributesForSave($value, $relation, $item->getKey(), $keyName, $relationMethod);
if (isset($value[$keyName])) {
$relation->newPivot()->where($keyName, $value[$keyName])->update($attributes);
} else {
Expand Down Expand Up @@ -188,16 +188,16 @@ private function createRelationsForItem($item, $formattedRelations)
}
}

private function preparePivotAttributesForSave(array $attributes, BelongsToMany|MorphToMany $relation, string|int $relatedItemKey, $pivotKeyName)
private function preparePivotAttributesForSave(array $attributes, BelongsToMany|MorphToMany $relation, string|int $relatedItemKey, $pivotKeyName, $relationMethod): array
{
$attributes[$relation->getForeignPivotKeyName()] = $relatedItemKey;
$attributes[$relation->getRelatedPivotKeyName()] = $attributes[$relation->getRelationName()];
$attributes[$relation->getRelatedPivotKeyName()] = $attributes[$relationMethod];

if ($relation instanceof MorphToMany) {
$attributes[$relation->getMorphType()] = $relation->getMorphClass();
}

return Arr::except($attributes, [$relation->getRelationName(), $pivotKeyName]);
return Arr::except($attributes, [$relationMethod, $pivotKeyName]);
}

/**
Expand Down
125 changes: 119 additions & 6 deletions tests/Unit/CrudPanel/CrudPanelCreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,51 @@ public function testBelongsToManyWithPivotDataRelationship()
$this->assertEquals('my first article note', $entry->fresh()->superArticles->first()->pivot->notes);
}

public function testBelongsToManyWithPivotDataDynamicRelationship()
{
User::resolveRelationUsing('dynamicRelation', function ($user) {
return $user->belongsToMany(\Backpack\CRUD\Tests\config\Models\Article::class, 'articles_user')->withPivot(['notes', 'start_date', 'end_date']);
});

$this->crudPanel->setModel(User::class);
$this->crudPanel->addFields($this->userInputFieldsNoRelationships);
$this->crudPanel->addField([
'name' => 'dynamicRelation',
'subfields' => [
[
'name' => 'notes',
],
],
]);

$faker = Factory::create();
$articleData = [
'content' => $faker->text(),
'tags' => $faker->words(3, true),
'user_id' => 1,
];

$article = Article::create($articleData);

$inputData = [
'name' => $faker->name,
'email' => $faker->safeEmail,
'password' => Hash::make($faker->password()),
'remember_token' => null,
'dynamicRelation' => [
[
'dynamicRelation' => $article->id,
'notes' => 'my first article note',
],
],
];

$entry = $this->crudPanel->create($inputData);

$this->assertCount(1, $entry->fresh()->dynamicRelation);
$this->assertEquals('my first article note', $entry->fresh()->dynamicRelation->first()->pivot->notes);
}

public function testBelongsToManyWithMultipleSameRelationIdAndPivotDataRelationship()
{
$inputData = $this->getPivotInputData(['superArticlesDuplicates' => [
Expand Down Expand Up @@ -712,9 +757,9 @@ public function testBelongsToManyWithMultipleSameRelationIdAndPivotDataRelations
$entry = $entry->fresh();

$this->assertCount(3, $entry->superArticlesDuplicates);
$this->assertEquals('my first article note', $entry->superArticles->first()->pivot->notes);
$this->assertEquals('my second article note', $entry->superArticles[1]->pivot->notes);
$this->assertEquals('my first article2 note', $entry->superArticles[2]->pivot->notes);
$this->assertEquals('my first article note', $entry->superArticlesDuplicates->first()->pivot->notes);
$this->assertEquals('my second article note', $entry->superArticlesDuplicates[1]->pivot->notes);
$this->assertEquals('my first article2 note', $entry->superArticlesDuplicates[2]->pivot->notes);

$inputData = $this->getPivotInputData(['superArticlesDuplicates' => [
[
Expand Down Expand Up @@ -742,9 +787,77 @@ public function testBelongsToManyWithMultipleSameRelationIdAndPivotDataRelations
$entry = $entry->fresh();

$this->assertCount(3, $entry->superArticlesDuplicates);
$this->assertEquals('my first article note updated', $entry->superArticles[0]->pivot->notes);
$this->assertEquals('my second article note updated', $entry->superArticles[1]->pivot->notes);
$this->assertEquals('my first article2 note updated', $entry->superArticles[2]->pivot->notes);
$this->assertEquals('my first article note updated', $entry->superArticlesDuplicates[0]->pivot->notes);
$this->assertEquals('my second article note updated', $entry->superArticlesDuplicates[1]->pivot->notes);
$this->assertEquals('my first article2 note updated', $entry->superArticlesDuplicates[2]->pivot->notes);
}

public function testBelongsToManyWithMultipleSameRelationIdAndPivotDataDynamicRelationship()
{
User::resolveRelationUsing('dynamicRelation', function ($user) {
return $user->belongsToMany(\Backpack\CRUD\Tests\config\Models\Article::class, 'articles_user')->withPivot(['notes', 'start_date', 'end_date', 'id'])->using('Backpack\CRUD\Tests\config\Models\SuperArticlePivot');
});

$inputData = $this->getPivotInputData(['dynamicRelation' => [
[
'dynamicRelation' => 1,
'notes' => 'my first article note',
'id' => null,
],
[
'dynamicRelation' => 1,
'notes' => 'my second article note',
'id' => null,
],
[
'dynamicRelation' => 2,
'notes' => 'my first article2 note',
'id' => null,
],
],
], true, true);

$entry = $this->crudPanel->create($inputData);
$relationField = $this->crudPanel->getUpdateFields($entry->id)['dynamicRelation'];

$this->assertCount(3, $relationField['value']);

$entry = $entry->fresh();

$this->assertCount(3, $entry->dynamicRelation);
$this->assertEquals('my first article note', $entry->dynamicRelation->first()->pivot->notes);
$this->assertEquals('my second article note', $entry->dynamicRelation[1]->pivot->notes);
$this->assertEquals('my first article2 note', $entry->dynamicRelation[2]->pivot->notes);

$inputData = $this->getPivotInputData(['dynamicRelation' => [
[
'dynamicRelation' => 1,
'notes' => 'my first article note updated',
'id' => 1,
],
[
'dynamicRelation' => 1,
'notes' => 'my second article note updated',
'id' => 2,
],
[
'dynamicRelation' => 2,
'notes' => 'my first article2 note updated',
'id' => 3,
],
],
], false, true);

$entry = $this->crudPanel->update($entry->id, $inputData);
$relationField = $this->crudPanel->getUpdateFields($entry->id)['dynamicRelation'];
$this->assertCount(3, $relationField['value']);

$entry = $entry->fresh();

$this->assertCount(3, $entry->dynamicRelation);
$this->assertEquals('my first article note updated', $entry->dynamicRelation[0]->pivot->notes);
$this->assertEquals('my second article note updated', $entry->dynamicRelation[1]->pivot->notes);
$this->assertEquals('my first article2 note updated', $entry->dynamicRelation[2]->pivot->notes);
}

public function testBelongsToManyAlwaysSaveSinglePivotWhenMultipleNotAllowed()
Expand Down

0 comments on commit e38cc68

Please sign in to comment.