]> _ Git - cubist_cms-back.git/commitdiff
wip #5150 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 8 Mar 2022 21:47:56 +0000 (22:47 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 8 Mar 2022 21:47:56 +0000 (22:47 +0100)
src/app/Console/Commands/RefreshComposedAttributesCommand.php [new file with mode: 0644]
src/app/CubistBackpackServiceProvider.php
src/app/Magic/Models/CubistMagicAbstractModel.php

diff --git a/src/app/Console/Commands/RefreshComposedAttributesCommand.php b/src/app/Console/Commands/RefreshComposedAttributesCommand.php
new file mode 100644 (file)
index 0000000..4904840
--- /dev/null
@@ -0,0 +1,53 @@
+<?php
+
+namespace Cubist\Backpack\Console\Commands;
+
+use Cubist\Backpack\Magic\Models\CubistMagicAbstractModel;
+use Doctrine\DBAL\DriverManager;
+use Doctrine\DBAL\Schema\Comparator;
+use Doctrine\DBAL\Schema\Schema;
+use Illuminate\Support\Str;
+use Spatie\MediaLibrary\MediaCollections\Models\Media;
+
+class RefreshComposedAttributesCommand extends CubistMagicCommand
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'cubist:magic:refreshcomposedattributes';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Make database migrations for Magic Models';
+    protected $_schemas = [];
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        $this->_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) {
+
+        }
+    }
+}
index 466001d74f213a93d5f653aff5632685add41c67..1e610cd0c0bfdd2e24a798a1e0c17ebbb6896fb7 100644 (file)
@@ -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
index cb0104563a6cc2b953cc328275381b95d3ccf950..3ba4fc9ff0412770f6c61ca0ee7fb8282ada5ad5 100644 (file)
@@ -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();
         }
     }