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

Improve related save #2599

Merged
merged 5 commits into from
Oct 14, 2024
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
26 changes: 17 additions & 9 deletions src/Models/Behaviors/HasRelated.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace A17\Twill\Models\Behaviors;

use A17\Twill\Models\Contracts\TwillModelContract;
use A17\Twill\Models\RelatedItem;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;

trait HasRelated
Expand Down Expand Up @@ -52,6 +51,7 @@ public function loadRelated(string $browserName): Collection
/** @var \A17\Twill\Models\Model $model */
if ($model = $item->related) {
$model->setRelation('pivot', $item);
$item->unsetRelation('related');

return $model;
}
Expand All @@ -63,18 +63,26 @@ public function loadRelated(string $browserName): Collection
/**
* Attach items to the model for a browser field.
*
* @param array<int, TwillModelContract> $items
* @param array<int, Model|array> $items
*/
public function saveRelated(array|Collection $items, string $browserName): void
public function saveRelated(array|Collection|Model $items, string $browserName): void
{
$items = is_array($items) || $items instanceof Collection ? $items : [$items];

/** @var Collection<int, RelatedItem> $itemsToProcess */
$itemsToProcess = $this->relatedItems()->where('browser_name', $browserName)->get();

foreach ($items as $position => $item) {
if ($item instanceof Model) {
$id = $item->getKey();
$type = $item->getMorphClass();
} else {
$id = $item['id'];
$type = $item['endpointType'];
}

$firstMatchKey = $itemsToProcess
->where('related_id', $item['id'])
->where('related_type', $item['endpointType'])
->where('browser_name', $browserName)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is already taken care of in line 71

->where(fn (RelatedItem $item) => $item->related_id == $id && $item->related_type === $type)
// We should only have one item always as you cannot select the same items twice.
->keys()
->first();
Expand All @@ -90,8 +98,8 @@ public function saveRelated(array|Collection $items, string $browserName): void
RelatedItem::create([
'subject_id' => $this->getKey(),
'subject_type' => $this->getMorphClass(),
'related_id' => $item['id'],
'related_type' => $item['endpointType'],
'related_id' => $id,
'related_type' => $type,
'browser_name' => $browserName,
'position' => $position + 1,
]);
Expand Down
7 changes: 1 addition & 6 deletions tests/integration/Controllers/Tables/BrowserColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@ public function testWithSingleValue(): void
$category = $this->createCategory();

$this->author->saveRelated(
[
[
'id' => $category->id,
'endpointType' => Category::class,
],
],
$category,
'categories'
);

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/NplusOneRelatedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testSingleRelatedItem(): void
'published' => true,
]);

$storeArray[] = ['endpointType' => Writer::class, 'id' => $writer->id];
$storeArray[] = $writer;
}

$letter->saveRelated($storeArray, 'dummyBrowser');
Expand Down
Loading