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

Fix BlockRepository setting relation of an object and improve amount … #2672

Merged
merged 3 commits into from
Oct 21, 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
6 changes: 2 additions & 4 deletions src/Repositories/Behaviors/HandleRevisions.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,9 @@ public function hydrateRelatedBrowsers(TwillModelContract $object, array $fields

foreach ($relatedBrowsers as $browser) {
$browserField = $fields['browsers'][$browser['browserName']] ?? [];

$position = 1;
foreach ($browserField as $values) {
$position = 1;

$relatedBrowserItems->push(RelatedItem::make([
$relatedBrowserItems->push(new RelatedItem([
'subject_id' => $object->getKey(),
'subject_type' => $object->getMorphClass(),
'related_id' => $values['id'],
Expand Down
60 changes: 25 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,36 @@ 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) => new RelatedItem([
'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);
$model->loadMissing('relatedItems.related');

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();
Copy link
Contributor Author

@Tofandel Tofandel Oct 15, 2024

Choose a reason for hiding this comment

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

This would previously delete and recreate all related items, which is very write intensive on the database for nothing because saveRelated already does an existence check and deletes the deleted ones


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 +74,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
Loading