--- /dev/null
+<?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) {
+
+ }
+ }
+}
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;
UpdateCommand::class,
GenerateCommand::class,
MigrateCommand::class,
+ RefreshComposedAttributesCommand::class,
SearchIndexCommand::class,
LocaleCopy::class,
LocaleSlugReset::class
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;
else {
$saved = $this->performInsert($query);
- if (! $this->getConnectionName() &&
+ if (!$this->getConnectionName() &&
$connection = $query->getConnection()) {
$this->setConnection($connection->getName());
}
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();
}
}