/** * @return \yii\db\ActiveQuery */ public function getTranslations() { return $this->hasMany(MenuItemLang::className(), ['menu_item_id' => 'id']); }
protected function saveModel($model, $post) { // Wrap everything in a database transaction $transaction = Yii::$app->db->beginTransaction(); // Get the params $params = $this->getDefaultViewParams($model); $currentParentId = $model->parent_id; // Populate the model with the POST data $model->load($post); // Set level and calculate next position for a new record or when // the parent has changed if ($model->isNewRecord || !$model->isNewRecord && $currentParentId != $model->parent_id) { $model->level = $model->parent_id ? $model->parent->level + 1 : 0; $model->position = $model->nextPosition(); } // Validate the main model if (!$model->validate()) { return $this->render($this->action->id, $params); } // Add the translations foreach (Yii::$app->request->post(StringHelper::basename(MenuItemLang::className()), []) as $language => $data) { foreach ($data as $attribute => $translation) { $model->translate($language)->{$attribute} = $translation; } } // Save the main model if (!$model->save()) { return $this->render($this->action->id, $params); } $transaction->commit(); // Set flash message if ($this->action->id == 'create') { Yii::$app->getSession()->setFlash('menu-item', Yii::t('app', '"{item}" has been created', ['item' => $model->name])); } else { Yii::$app->getSession()->setFlash('menu-item', Yii::t('app', '"{item}" has been updated', ['item' => $model->name])); } // Take appropriate action based on the pushed button if (isset($post['save-close'])) { return $this->redirect(['index']); } elseif (isset($post['save-add'])) { return $this->redirect(['create']); } else { return $this->redirect(['update', 'id' => $model->id]); } }