Skip to content

Commit

Permalink
Fix BlockRepository setting relation of an object and improve amount …
Browse files Browse the repository at this point in the history
…of queries during saves
  • Loading branch information
Tofandel committed Oct 15, 2024
1 parent 3297066 commit e782fdf
Showing 1 changed file with 26 additions and 35 deletions.
61 changes: 26 additions & 35 deletions src/Repositories/BlockRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
use A17\Twill\Facades\TwillBlocks;
use A17\Twill\Models\Block;
use A17\Twill\Models\Contracts\TwillModelContract;
use A17\Twill\Models\RelatedItem;
use A17\Twill\Repositories\Behaviors\HandleFiles;
use A17\Twill\Repositories\Behaviors\HandleMedias;
use A17\Twill\Services\Blocks\Block as BlockConfig;
use Illuminate\Config\Repository as Config;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;
use ReflectionException;

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

public function hydrate(TwillModelContract $model, array $fields): TwillModelContract
{
if (Schema::hasTable(config('twill.related_table', 'twill_related'))) {
$relatedItems = Collection::make();

Collection::make($fields['browsers'])->each(function ($items, $browserName) use (&$relatedItems) {
Collection::make($items)->each(function ($item) use ($browserName, &$relatedItems) {
try {
// @todo: Repository could be null.
$repository = $this->getModelRepository($item['endpointType'] ?? $browserName);
$relatedItems->push(
(object) [
'related' => $repository->getById($item['id']),
'browser_name' => $browserName,
]
);
} catch (ReflectionException $reflectionException) {
Log::error($reflectionException);
}
});
});

$model->setRelation('relatedItems', $relatedItems);
}
$relatedItems = collect($fields['browsers'])
->flatMap(fn($items, $browserName) => collect($items)
->map(fn($item, $position) => RelatedItem::make([

Check failure on line 38 in src/Repositories/BlockRepository.php

View workflow job for this annotation

GitHub Actions / lint

Called 'Model::make()' which performs unnecessary work, use 'new Model()'.
'subject_id' => $model->getKey(),
'subject_type' => $model->getMorphClass(),
'related_id' => $item['id'],
'related_type' => $item['endpointType'],
'browser_name' => $browserName,
'position' => $position,
])
)
);

$model->setRelation('relatedItems', $relatedItems);

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

/** @param Block $model */
public function afterSave(TwillModelContract $model, array $fields): void
{
if (Schema::hasTable(config('twill.related_table', 'twill_related'))) {
if (!empty($fields['browsers'])) {
$browserNames = collect($fields['browsers'])->each(function ($items, $browserName) use ($model) {
// This will create items or delete them if they are missing
$model->saveRelated($items, $browserName);
})->keys();

// Delete all the related items that were emptied
RelatedItem::query()->whereMorphedTo('subject', $model)->whereNotIn('browser_name', $browserNames)->delete();
} else {
$model->clearAllRelated();

if (isset($fields['browsers'])) {
Collection::make($fields['browsers'])->each(function ($items, $browserName) use ($model) {
$model->saveRelated($items, $browserName);
});
}
}

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

if (Schema::hasTable(config('twill.related_table', 'twill_related'))) {
$object->clearAllRelated();
}
$object->clearAllRelated();
}

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

0 comments on commit e782fdf

Please sign in to comment.