From: Vincent Vanwaelscappel Date: Wed, 5 Jun 2019 14:14:14 +0000 (+0200) Subject: #2810 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=b9f3f5844c9cf7b5e6aea1e0d46012f2e0a12acf;p=cubist_cms-back.git #2810 --- diff --git a/src/app/Magic/Fields/FieldsCollection.php b/src/app/Magic/Fields/FieldsCollection.php new file mode 100644 index 0000000..044cfd3 --- /dev/null +++ b/src/app/Magic/Fields/FieldsCollection.php @@ -0,0 +1,14 @@ + false]); + return array_merge(parent::getDefaultAttributes(), ['fillable' => false, 'pivot' => true]); } } diff --git a/src/app/Magic/Models/CubistMagicAbstractModel.php b/src/app/Magic/Models/CubistMagicAbstractModel.php index 4f1ebb4..dc568de 100644 --- a/src/app/Magic/Models/CubistMagicAbstractModel.php +++ b/src/app/Magic/Models/CubistMagicAbstractModel.php @@ -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()]); } }