Skip to content

Commit e782fdf

Browse files
committed
Fix BlockRepository setting relation of an object and improve amount of queries during saves
1 parent 3297066 commit e782fdf

File tree

1 file changed

+26
-35
lines changed

1 file changed

+26
-35
lines changed

src/Repositories/BlockRepository.php

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55
use A17\Twill\Facades\TwillBlocks;
66
use A17\Twill\Models\Block;
77
use A17\Twill\Models\Contracts\TwillModelContract;
8+
use A17\Twill\Models\RelatedItem;
89
use A17\Twill\Repositories\Behaviors\HandleFiles;
910
use A17\Twill\Repositories\Behaviors\HandleMedias;
1011
use A17\Twill\Services\Blocks\Block as BlockConfig;
1112
use Illuminate\Config\Repository as Config;
1213
use Illuminate\Support\Collection;
13-
use Illuminate\Support\Facades\Log;
14-
use Illuminate\Support\Facades\Schema;
15-
use ReflectionException;
1614

1715
class BlockRepository extends ModuleRepository
1816
{
@@ -35,42 +33,37 @@ public function getCrops(string $role): array
3533

3634
public function hydrate(TwillModelContract $model, array $fields): TwillModelContract
3735
{
38-
if (Schema::hasTable(config('twill.related_table', 'twill_related'))) {
39-
$relatedItems = Collection::make();
40-
41-
Collection::make($fields['browsers'])->each(function ($items, $browserName) use (&$relatedItems) {
42-
Collection::make($items)->each(function ($item) use ($browserName, &$relatedItems) {
43-
try {
44-
// @todo: Repository could be null.
45-
$repository = $this->getModelRepository($item['endpointType'] ?? $browserName);
46-
$relatedItems->push(
47-
(object) [
48-
'related' => $repository->getById($item['id']),
49-
'browser_name' => $browserName,
50-
]
51-
);
52-
} catch (ReflectionException $reflectionException) {
53-
Log::error($reflectionException);
54-
}
55-
});
56-
});
57-
58-
$model->setRelation('relatedItems', $relatedItems);
59-
}
36+
$relatedItems = collect($fields['browsers'])
37+
->flatMap(fn($items, $browserName) => collect($items)
38+
->map(fn($item, $position) => RelatedItem::make([
39+
'subject_id' => $model->getKey(),
40+
'subject_type' => $model->getMorphClass(),
41+
'related_id' => $item['id'],
42+
'related_type' => $item['endpointType'],
43+
'browser_name' => $browserName,
44+
'position' => $position,
45+
])
46+
)
47+
);
48+
49+
$model->setRelation('relatedItems', $relatedItems);
6050

6151
return parent::hydrate($model, $fields);
6252
}
6353

54+
/** @param Block $model */
6455
public function afterSave(TwillModelContract $model, array $fields): void
6556
{
66-
if (Schema::hasTable(config('twill.related_table', 'twill_related'))) {
57+
if (!empty($fields['browsers'])) {
58+
$browserNames = collect($fields['browsers'])->each(function ($items, $browserName) use ($model) {
59+
// This will create items or delete them if they are missing
60+
$model->saveRelated($items, $browserName);
61+
})->keys();
62+
63+
// Delete all the related items that were emptied
64+
RelatedItem::query()->whereMorphedTo('subject', $model)->whereNotIn('browser_name', $browserNames)->delete();
65+
} else {
6766
$model->clearAllRelated();
68-
69-
if (isset($fields['browsers'])) {
70-
Collection::make($fields['browsers'])->each(function ($items, $browserName) use ($model) {
71-
$model->saveRelated($items, $browserName);
72-
});
73-
}
7467
}
7568

7669
parent::afterSave($model, $fields);
@@ -82,9 +75,7 @@ public function afterDelete(TwillModelContract $object): void
8275
$object->medias()->detach();
8376
$object->files()->detach();
8477

85-
if (Schema::hasTable(config('twill.related_table', 'twill_related'))) {
86-
$object->clearAllRelated();
87-
}
78+
$object->clearAllRelated();
8879
}
8980

9081
public function buildFromCmsArray(array $block, bool $repeater = false): array

0 commit comments

Comments
 (0)