Example #1
0
 /**
  * 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]);
     }
 }
 public function actionUpdatex($id)
 {
     $model = $this->findModel($id);
     $modelUser = $this->findModelUser($model->user_id);
     $model->scenario = 'updateprofile';
     $model->password = $model->password_hash;
     $model->confirm_password = $model->password_hash;
     $oldPass = $model->password_hash;
     if ($model->load(Yii::$app->request->post()) && $modelUser->load(Yii::$app->request->post()) && Model::validateMultiple([$model, $modelUser])) {
         if ($oldPass !== $model->password) {
             $model->setPassword($model->password);
         }
         if ($modelUser->save()) {
             $model->save();
         }
         Yii::$app->getSession()->setFlash('success', 'บันทึกเสร็จเรียบร้อย');
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('update', ['model' => $model, 'modelUser' => $modelUser]);
     }
 }
 /**
  * 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]);
     }
 }