From 5f74022d385c16dec39bacea8b2ca5e6b020e059 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Tue, 8 Mar 2022 22:47:56 +0100 Subject: [PATCH] wip #5150 @0.5 --- .../RefreshComposedAttributesCommand.php | 53 +++++++++++++++++++ src/app/CubistBackpackServiceProvider.php | 2 + .../Magic/Models/CubistMagicAbstractModel.php | 22 +++++--- 3 files changed, 69 insertions(+), 8 deletions(-) create mode 100644 src/app/Console/Commands/RefreshComposedAttributesCommand.php diff --git a/src/app/Console/Commands/RefreshComposedAttributesCommand.php b/src/app/Console/Commands/RefreshComposedAttributesCommand.php new file mode 100644 index 0000000..4904840 --- /dev/null +++ b/src/app/Console/Commands/RefreshComposedAttributesCommand.php @@ -0,0 +1,53 @@ +_handleMagicFolder([$this, '_refresh']); + + } + + /** + * @param $model CubistMagicAbstractModel + */ + public function _refresh($model) + { + $this->line('Handling ' . get_class($model)); + + try { + call_user_func(get_class($model), 'refreshComposedAttributes'); + } catch (\Exception $e) { + + } + } +} diff --git a/src/app/CubistBackpackServiceProvider.php b/src/app/CubistBackpackServiceProvider.php index 466001d..1e610cd 100644 --- a/src/app/CubistBackpackServiceProvider.php +++ b/src/app/CubistBackpackServiceProvider.php @@ -7,6 +7,7 @@ use Cubist\Backpack\Console\Commands\GenerateCommand; use Cubist\Backpack\Console\Commands\InstallCommand; use Cubist\Backpack\Console\Commands\LocaleCopy; use Cubist\Backpack\Console\Commands\LocaleSlugReset; +use Cubist\Backpack\Console\Commands\RefreshComposedAttributesCommand; use Cubist\Backpack\Console\Commands\SearchIndexCommand; use Cubist\Backpack\Console\Commands\UpdateCommand; use Illuminate\Support\Facades\Blade; @@ -81,6 +82,7 @@ class CubistBackpackServiceProvider extends ServiceProvider UpdateCommand::class, GenerateCommand::class, MigrateCommand::class, + RefreshComposedAttributesCommand::class, SearchIndexCommand::class, LocaleCopy::class, LocaleSlugReset::class diff --git a/src/app/Magic/Models/CubistMagicAbstractModel.php b/src/app/Magic/Models/CubistMagicAbstractModel.php index cb01045..3ba4fc9 100644 --- a/src/app/Magic/Models/CubistMagicAbstractModel.php +++ b/src/app/Magic/Models/CubistMagicAbstractModel.php @@ -940,10 +940,10 @@ class CubistMagicAbstractModel extends Model implements HasMedia protected function getAttributesForInsert() { - $res=parent::getAttributesForInsert(); - foreach ($res as $k=>$v) { - if(is_array($v) || is_object($v)){ - $res[$k]=json_encode($v); + $res = parent::getAttributesForInsert(); + foreach ($res as $k => $v) { + if (is_array($v) || is_object($v)) { + $res[$k] = json_encode($v); } } return $res; @@ -1152,7 +1152,7 @@ class CubistMagicAbstractModel extends Model implements HasMedia else { $saved = $this->performInsert($query); - if (! $this->getConnectionName() && + if (!$this->getConnectionName() && $connection = $query->getConnection()) { $this->setConnection($connection->getName()); } @@ -1168,14 +1168,20 @@ class CubistMagicAbstractModel extends Model implements HasMedia return $saved; } - public static function refreshComposedAttributes(){ - $all=static::all(); + public static function refreshComposedAttributes() + { + $all = static::all(); + $hasComposed = false; foreach ($all as $instance) { foreach ($instance->getFields() as $field) { - if($field instanceof Composed){ + if ($field instanceof Composed) { + $hasComposed = true; $instance->getAttribute($field->getName()); } } + if (!$hasComposed) { + return; + } $instance->save(); } } -- 2.39.5