/** * Creates a new Po model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Po(); $modelsPoItem = [new PoItem()]; if ($model->load(Yii::$app->request->post())) { $modelsPoItem = Model::createMultiple(PoItem::classname()); Model::loadMultiple($modelsPoItem, Yii::$app->request->post()); // ajax validation /*if (Yii::$app->request->isAjax) { Yii::$app->response->format = Response::FORMAT_JSON; return ArrayHelper::merge( ActiveForm::validateMultiple($modelsPoItem), ActiveForm::validate($model) ); }*/ // validate all models $valid = $model->validate(); $valid = Model::validateMultiple($modelsPoItem) && $valid; if ($valid) { $transaction = \Yii::$app->db->beginTransaction(); try { if ($flag = $model->save(false)) { foreach ($modelsPoItem as $modelPoItem) { $modelPoItem->po_id = $model->id; if (!($flag = $modelPoItem->save(false))) { $transaction->rollBack(); break; } } } if ($flag) { $transaction->commit(); return $this->redirect(['view', 'id' => $model->id]); } } catch (Exception $e) { $transaction->rollBack(); } } } else { return $this->render('create', ['model' => $model, 'modelsPoItem' => empty($modelsPoItem) ? [new PoItem()] : $modelsPoItem]); } }
/** * Creates a new Pelicula model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Pelicula(); $modelActor = [new Actorpelicula()]; $modelDirector = [new Directorpelicula()]; $modeltipoPelicula = [new Tipopeliculapelicula()]; if ($model->load(Yii::$app->request->post()) && $model->save()) { $modelActor = Model::createMultiple(Actorpelicula::classname()); Model::loadMultiple($modelActor, Yii::$app->request->post()); $modelDirector = Model::createMultiple(Directorpelicula::classname()); Model::loadMultiple($modelDirector, Yii::$app->request->post()); $modeltipoPelicula = Model::createMultiple(Tipopeliculapelicula::classname()); Model::loadMultiple($modeltipoPelicula, Yii::$app->request->post()); $valid = $model->validate(); $valid = Model::validateMultiple($modelActor) && $valid; $valid = Model::validateMultiple($modelDirector) && $valid; $valid = Model::validateMultiple($modeltipoPelicula) && $valid; return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } }
/** * Updates an existing Image model. * If update is successful, the browser will be redirected to the 'view' page. * @param string $id * @return mixed */ public function actionUpdate($id, $sliderId) { $model = $this->findModel($id); $slider = Slider::findOne($sliderId); if (Yii::$app->request->getIsPost()) { $post = Yii::$app->request->post(); // Ajax request, validate the models if (Yii::$app->request->isAjax) { // Populate the model with the POST data $model->load($post); // Create an array of translation models and populate them $translationModels = ArrayHelper::index($model->getTranslations()->all(), 'language'); Model::loadMultiple($translationModels, $post); // Validate the model and translation $response = array_merge(ActiveForm::validate($model), ActiveForm::validateMultiple($translationModels)); // Return validation in JSON format Yii::$app->response->format = Response::FORMAT_JSON; return $response; // Normal request, save models } else { // Wrap everything in a database transaction $transaction = Yii::$app->db->beginTransaction(); // Validate the main model if (!$model->load($post)) { return $this->render('update', ['model' => $model, 'slider' => $slider]); } // Add the translations foreach (Yii::$app->request->post(StringHelper::basename(ImageLang::className()), []) as $language => $data) { foreach ($data as $attribute => $translation) { $model->translate($language)->{$attribute} = $translation; } } if (!$model->save()) { return $this->render('update', ['model' => $model, 'slider' => $slider]); } $transaction->commit(); // Set flash message Yii::$app->getSession()->setFlash('image-success', Yii::t('app', '"{item}" has been updated', ['item' => $model->name])); return $this->redirect(['index?sliderId=' . $slider->id]); } } return $this->render('update', ['model' => $model, 'slider' => $slider]); }