public function actionCreateMultipleModels() { $models = []; $OK = true; if (isset($_POST['Customer'])) { foreach ($_POST['Customer'] as $postObj) { $customer = new Customer(); $customer->attributes = $postObj; $isValid = $customer->validate(); $OK = $OK && $isValid; $models[] = $customer; } if ($OK) { foreach ($models as $model) { $model->save(); } $this->redirect('customers/grid'); } } else { for ($i = 0; $i < 3; $i++) { $models[] = new Customer(); } } return $this->render('createMultipleModels', compact('models')); }
public function actionCreateMultipleModels() { $models = []; if (isset($_POST['Customer'])) { $validateOK = true; foreach ($_POST['Customer'] as $postObj) { $model = new Customer(); $model->attributes = $postObj; $models[] = $model; $validateOK = $validateOK && $model->validate(); } // All models are validated and will be saved if ($validateOK) { foreach ($models as $model) { $model->save(); } // Redirect to grid after save return $this->redirect(['grid']); } } else { for ($k = 0; $k < 3; $k++) { $models[] = new Customer(); } } return $this->render('createMultipleModels', ['models' => $models]); }
public function actionCreateCustomerAndReservation() { $customer = new Customer(); $reservation = new Reservation(); $reservation->customer_id = 0; if ($customer->load(Yii::$app->request->post()) && $reservation->load(Yii::$app->request->post()) && $customer->validate() && $reservation->validate()) { $dbTrans = Yii::$app->db->beginTransaction(); $customerSaved = $customer->save(); if ($customerSaved) { $reservation->customer_id = $customer->id; $reservationSaved = $reservation->save(); if ($reservationSaved) { $dbTrans->commit(); } else { $dbTrans->rollback(); } } else { $dbTrans->rollback(); } } return $this->render('createCustomerAndReservation', ['customer' => $customer, 'reservation' => $reservation]); }
public function actionCreateCustomerAndReservation() { $customer = new \app\models\Customer(); $reservation = new \app\models\Reservation(); // It is useful to set fake customer_id to reservation model to avoid validationerror (because customer_id is mandatory) $reservation->customer_id = 0; if ($customer->load(Yii::$app->request->post()) && $reservation->load(Yii::$app->request->post()) && $customer->validate() && $reservation->validate()) { $dbTrans = Yii::$app->db->beginTransaction(); $customerSaved = $customer->save(); if ($customerSaved) { $reservation->customer_id = $customer->id; $reservationSaved = $reservation->save(); if ($reservationSaved) { $dbTrans->commit(); } else { $dbTrans->rollback(); } } else { $dbTrans->rollback(); } } return $this->render('createCustomerAndReservation', ['customer' => $customer, 'reservation' => $reservation]); }