diff --git a/modules/backend/behaviors/FormController.php b/modules/backend/behaviors/FormController.php index 7134225e7..981671e14 100644 --- a/modules/backend/behaviors/FormController.php +++ b/modules/backend/behaviors/FormController.php @@ -44,6 +44,11 @@ class FormController extends ControllerBehavior */ protected $context; + /** + * @var array List of prepared models that require saving. + */ + private $modelsToSave = []; + /** * Behavior constructor * @param Backend\Classes\Controller $controller @@ -134,14 +139,17 @@ class FormController extends ControllerBehavior */ public function create_onSave() { + $this->context = $this->getConfig('update[context]', 'create'); $model = $this->controller->formCreateModelObject(); $this->initForm($model); $this->controller->formBeforeSave($model); $this->controller->formBeforeCreate($model); - $this->setModelAttributes($model, $this->formWidget->getSaveData()); - $model->push($this->formWidget->getSessionKey()); + $modelsToSave = $this->prepareModelsToSave($model, $this->formWidget->getSaveData()); + foreach ($modelsToSave as $modelToSave) { + $modelToSave->save(null, $this->formWidget->getSessionKey()); + } $this->controller->formAfterSave($model); $this->controller->formAfterCreate($model); @@ -184,14 +192,17 @@ class FormController extends ControllerBehavior */ public function update_onSave($recordId = null) { + $this->context = $this->getConfig('update[context]', 'update'); $model = $this->controller->formFindModelObject($recordId); - $this->initForm($model, 'update'); + $this->initForm($model); $this->controller->formBeforeSave($model); $this->controller->formBeforeUpdate($model); - $this->setModelAttributes($model, $this->formWidget->getSaveData()); - $model->push($this->formWidget->getSessionKey()); + $modelsToSave =$this->prepareModelsToSave($model, $this->formWidget->getSaveData()); + foreach ($modelsToSave as $modelToSave) { + $modelToSave->save(null, $this->formWidget->getSessionKey()); + } $this->controller->formAfterSave($model); $this->controller->formAfterUpdate($model); @@ -209,8 +220,9 @@ class FormController extends ControllerBehavior */ public function update_onDelete($recordId = null) { + $this->context = $this->getConfig('update[context]', 'update'); $model = $this->controller->formFindModelObject($recordId); - $this->initForm($model, 'update'); + $this->initForm($model); $model->delete(); @@ -556,14 +568,23 @@ class FormController extends ControllerBehavior // Internals // + private function prepareModelsToSave($model, $saveData) + { + $this->modelsToSave = []; + $this->setModelAttributes($model, $saveData); + return $this->modelsToSave; + } + /** * Sets a data collection to a model attributes, relations will also be set. * @param array $saveData Data to save. * @param Model $model Model to save to - * @return void + * @return array The collection of models to save. */ private function setModelAttributes($model, $saveData) { + $this->modelsToSave[] = $model; + if (!is_array($saveData)) return;