From 954ca4df55253c0b2123d9aae848e8ded2682750 Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Wed, 4 Oct 2023 16:27:41 +0100 Subject: [PATCH] wip --- src/Actions/MaybeRefreshDatabaseContent.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Actions/MaybeRefreshDatabaseContent.php b/src/Actions/MaybeRefreshDatabaseContent.php index da235a6..7b9148e 100644 --- a/src/Actions/MaybeRefreshDatabaseContent.php +++ b/src/Actions/MaybeRefreshDatabaseContent.php @@ -16,7 +16,7 @@ class MaybeRefreshDatabaseContent public function shouldRefresh(Orbit&Model $model): bool { $databaseMTime = filemtime(config('orbit.paths.database')); - $directory = config('orbit.paths.content').DIRECTORY_SEPARATOR.$model->getOrbitSource(); + $directory = config('orbit.paths.content') . DIRECTORY_SEPARATOR . $model->getOrbitSource(); $highestMTime = 0; foreach (new FilesystemIterator($directory, FilesystemIterator::SKIP_DOTS) as $file) { @@ -30,7 +30,7 @@ public function shouldRefresh(Orbit&Model $model): bool public function refresh(Orbit&Model $model, Driver $driver): void { - $directory = config('orbit.paths.content').DIRECTORY_SEPARATOR.$model->getOrbitSource(); + $directory = config('orbit.paths.content') . DIRECTORY_SEPARATOR . $model->getOrbitSource(); $iterator = new FilesystemIterator($directory, FilesystemIterator::SKIP_DOTS); $records = []; @@ -50,9 +50,17 @@ public function refresh(Orbit&Model $model, Driver $driver): void // This will ensure that we don't have any collisions with existing data in the SQLite database. $model->query()->whereKey($chunk->pluck($model->getKey())->all())->delete(); - $model->insert( + $model->query()->insert( $chunk - ->map(fn (array $attributes) => FillMissingAttributeValuesFromBlueprint::fill($attributes, $blueprint)) + ->map(function (array $attributes) use ($model, $blueprint) { + foreach ($attributes as $key => $value) { + $model->setAttribute($key, $value); + } + + $attributes = array_filter($model->getAttributes(), fn (string $key) => array_key_exists($key, $attributes), ARRAY_FILTER_USE_KEY); + + return FillMissingAttributeValuesFromBlueprint::fill($attributes, $blueprint); + }) ->all() ); });