]> _ Git - cubist_cms-back.git/commitdiff
#2810
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 5 Jun 2019 14:14:14 +0000 (16:14 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 5 Jun 2019 14:14:14 +0000 (16:14 +0200)
src/app/Magic/Fields/FieldsCollection.php [new file with mode: 0644]
src/app/Magic/Fields/SelectFromModelMultiple.php
src/app/Magic/Models/CubistMagicAbstractModel.php

diff --git a/src/app/Magic/Fields/FieldsCollection.php b/src/app/Magic/Fields/FieldsCollection.php
new file mode 100644 (file)
index 0000000..044cfd3
--- /dev/null
@@ -0,0 +1,14 @@
+<?php
+
+namespace Cubist\Backpack\app\Magic\Fields;
+
+class FieldsCollection extends Field
+{
+    protected $_adminType = 'collection';
+    protected $_databaseType = 'text';
+
+    public function setDefinitions()
+    {
+
+    }
+}
index 5accf7cd65f86a827d039debd3644e4ee0aca0fd..a069c736afbacb93bcdb7284d740a1b67015df08 100644 (file)
@@ -15,6 +15,6 @@ class SelectFromModelMultiple extends SelectFromModel
 
     public function getDefaultAttributes()
     {
-        return array_merge(parent::getDefaultAttributes(), ['fillable' => false]);
+        return array_merge(parent::getDefaultAttributes(), ['fillable' => false, 'pivot' => true]);
     }
 }
index 4f1ebb43a2db591f82b2c731975548b47577c482..dc568defcb5b7ba0bedf6aa2de81428045ef395d 100644 (file)
@@ -66,6 +66,34 @@ class CubistMagicAbstractModel extends Model
         $this->fill($attributes);
     }
 
+    /**
+     * Create translated items as json.
+     *
+     * @param array $attributes
+     * @return static
+     */
+    public static function create(array $attributes = [])
+    {
+        $locale = $attributes['locale'] ?? \App::getLocale();
+        $attributes = array_except($attributes, ['locale']);
+        $non_translatable = [];
+
+        $model = new static();
+
+        // do the actual saving
+        foreach ($attributes as $attribute => $value) {
+            if ($model->isTranslatableAttribute($attribute)) { // the attribute is translatable
+                $model->setTranslation($attribute, $locale, $value);
+            } else { // the attribute is NOT translatable
+                $non_translatable[$attribute] = $value;
+            }
+        }
+        $model->fill($non_translatable)->save();
+
+        return $model;
+    }
+
+
     public function setFields()
     {
 
@@ -260,9 +288,9 @@ class CubistMagicAbstractModel extends Model
     protected function _addTimestampsDatabaseColumns($table)
     {
         $options = ['notnull' => false];
-        $table->addColumn(static::CREATED_AT, 'date', $options);
-        $table->addColumn(static::UPDATED_AT, 'date', $options);
-        $table->addColumn('deleted_at', 'date', $options);
+        $table->addColumn(static::CREATED_AT, 'datetime', $options);
+        $table->addColumn(static::UPDATED_AT, 'datetime', $options);
+        $table->addColumn('deleted_at', 'datetime', $options);
     }
 
     public function replicate(array $except = null)
@@ -292,7 +320,7 @@ class CubistMagicAbstractModel extends Model
             case 'belongsTo':
                 return $this->belongsTo($field->getAttribute('model'), $field->getAttribute('name'));
             case 'belongsToMany':
-                return $this->belongsToMany($field->getAttribute('model'), $this->getRelationShipTable($field));
+                return $this->belongsToMany($field->getAttribute('model'), $this->getRelationShipTable($field))->withPivot([$this->getForeignKey(), self::_toModel($field->getAttribute('model'))->getForeignKey()]);
         }
     }