From: Vincent Vanwaelscappel Date: Wed, 3 Jul 2019 18:08:45 +0000 (+0200) Subject: #2843 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=ba38547154ed6fafb404b3e090d9bd31f6156389;p=cubist_cms-back.git #2843 --- diff --git a/src/app/Magic/Models/CMSPage.php b/src/app/Magic/Models/CMSPage.php index 15df6c8..40d5693 100644 --- a/src/app/Magic/Models/CMSPage.php +++ b/src/app/Magic/Models/CMSPage.php @@ -5,6 +5,8 @@ namespace Cubist\Backpack\app\Magic\Models; use Cubist\Backpack\app\Magic\Controllers\CubistMagicController; use Cubist\Backpack\app\Template\TemplateAbstract; +use Doctrine\DBAL\Schema\Schema; +use Doctrine\DBAL\Schema\Table; class CMSPage extends CubistMagicNestedModel { @@ -107,7 +109,7 @@ class CMSPage extends CubistMagicNestedModel * @param $controller CubistMagicController * @throws \Exception */ - protected function useTemplate($template, $controller) + protected function useTemplate($template, $controller = null) { if (is_string($template)) { $template = TemplateAbstract::getTemplateIntanceByName($template); @@ -124,7 +126,27 @@ class CMSPage extends CubistMagicNestedModel $attr = array_merge($this->defaultFieldAttributes, $field); $this->addFakeField($attr); } - $controller->updateFieldsFromModel(); + if (null !== $controller) { + $controller->updateFieldsFromModel(); + } + } + + /** + * @param $schema Schema + * @return Table + */ + public function setSchema($schema) + { + // We need to know all the columns that will be needed by all the templates + $this->useAllTemplates(); + parent::setSchema($schema); + } + + public function useAllTemplates() + { + foreach (TemplateAbstract::getTemplates() as $template) { + $this->useTemplate($template); + } } public function update(array $attributes = [], array $options = []) diff --git a/src/app/Magic/Models/CubistMagicAbstractModel.php b/src/app/Magic/Models/CubistMagicAbstractModel.php index 876f0ab..060336f 100644 --- a/src/app/Magic/Models/CubistMagicAbstractModel.php +++ b/src/app/Magic/Models/CubistMagicAbstractModel.php @@ -445,20 +445,35 @@ class CubistMagicAbstractModel extends Model implements HasMedia return Media::where('model_id', $this->getKey())->where('model_type', get_class($this))->get(); } - public function update(array $attributes = [], array $options = []) + protected function _prepareData($attributes) { + print_r($attributes); $attributes = Json::decodeRecursive($attributes, Json::TYPE_ARRAY); - return $this->updateTranslations($attributes, $options); + $res = []; + foreach ($attributes as $key => $attribute) { + if (is_array($attribute) || is_object($attribute)) { + $res[$key] = json_encode($attribute); + } else { + $res[$key] = $attribute; + } + } + return $res; + } + + public function update(array $attributes = [], array $options = []) + { + + return $this->updateTranslations($this->_prepareData($attributes), $options); } public function create(array $attributes = []) { - $attributes = Json::decodeRecursive($attributes, Json::TYPE_ARRAY); - return $this->createTranslations($attributes); + return $this->createTranslations($this->_prepareData($attributes)); } - public function getSlugAttribute($value){ - if(!$value){ + public function getSlugAttribute($value) + { + if (!$value) { return Str::slug($this->title); } return $value;