]> _ Git - cubist_cms-back.git/commitdiff
wip #5150 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 8 Mar 2022 18:14:49 +0000 (19:14 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 8 Mar 2022 18:14:49 +0000 (19:14 +0100)
src/app/Magic/Fields/Composed.php [new file with mode: 0644]
src/app/Magic/Models/CubistMagicAbstractModel.php

diff --git a/src/app/Magic/Fields/Composed.php b/src/app/Magic/Fields/Composed.php
new file mode 100644 (file)
index 0000000..0a40f7f
--- /dev/null
@@ -0,0 +1,13 @@
+<?php
+
+namespace Cubist\Backpack\Magic\Fields;
+
+class Composed extends Hidden
+{
+    protected string $_attribute = '';
+
+    public function getDefaultAttributes()
+    {
+        return array_merge(parent::getDefaultAttributes(), ['attribute' => $this->_attribute]);
+    }
+}
index d4a8453ee88ac75c9b97ff2dcbf5f01adebc9bae..8125dd5695a3e710b330a2922d7d371565b8c44b 100644 (file)
@@ -937,6 +937,16 @@ class CubistMagicAbstractModel extends Model implements HasMedia
         return $user->can($permission);
     }
 
+    protected function getAttributesForInsert()
+    {
+        $res=parent::getAttributesForInsert();
+        foreach ($res as $k=>$v) {
+            if(is_array($v) || is_object($v)){
+                $res[$k]=json_encode($v);
+            }
+        }
+        return $res;
+    }
 
     /**
      * @param $user CubistMagicAuthenticatable
@@ -1113,4 +1123,47 @@ class CubistMagicAbstractModel extends Model implements HasMedia
         }
         return null;
     }
+
+    public function save(array $options = [])
+    {
+        $this->mergeAttributesFromCachedCasts();
+
+        $query = $this->newModelQuery();
+
+        // If the "saving" event returns false we'll bail out of the save and return
+        // false, indicating that the save failed. This provides a chance for any
+        // listeners to cancel save operations if validations fail or whatever.
+        if ($this->fireModelEvent('saving') === false) {
+            return false;
+        }
+
+        // If the model already exists in the database we can just update our record
+        // that is already in this database using the current IDs in this "where"
+        // clause to only update this model. Otherwise, we'll just insert them.
+        if ($this->exists) {
+            $saved = $this->isDirty() ?
+                $this->performUpdate($query) : true;
+        }
+
+        // If the model is brand new, we'll insert it into our database and set the
+        // ID attribute on the model to the value of the newly inserted row's ID
+        // which is typically an auto-increment value managed by the database.
+        else {
+            $saved = $this->performInsert($query);
+
+            if (! $this->getConnectionName() &&
+                $connection = $query->getConnection()) {
+                $this->setConnection($connection->getName());
+            }
+        }
+
+        // If the model is successfully saved, we need to do a few more things once
+        // that is done. We will call the "saved" method here to run any actions
+        // we need to happen after a model gets successfully saved right here.
+        if ($saved) {
+            $this->finishSave($options);
+        }
+
+        return $saved;
+    }
 }