/** * Creates a new Subscriber model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Subscriber(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } }
/** * Creates a new Subscriber model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Subscriber(); $modelsPhone = [new Phone()]; if ($model->load(Yii::$app->request->post())) { $model->user_id = Yii::$app->user->getId(); if ($model->save()) { $modelsPhone = Model::createMultiple(Phone::classname()); Model::loadMultiple($modelsPhone, Yii::$app->request->post()); // validate all models $valid = $model->validate(); $valid = Model::validateMultiple($modelsPhone) && $valid; if ($valid) { $transaction = \Yii::$app->db->beginTransaction(); try { if ($flag = $model->save(false)) { foreach ($modelsPhone as $modelPhone) { $modelPhone->subscriber_id = $model->id; if (!($flag = $modelPhone->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, 'modelsPhone' => empty($modelsPhone) ? [new Phone()] : $modelsPhone]); } }