/** * Creates a new Purchase model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Purchase(['branch_id' => 1, 'date' => date('Y-m-d')]); $api = new ApiPurchase(['modelClass' => Purchase::className()]); //load gr list $greceipt = new \yii\data\ArrayDataProvider(); if ($model->load(Yii::$app->request->post())) { $transaction = Yii::$app->db->beginTransaction(); try { $data = $model->attributes; $data['details'] = Yii::$app->request->post('PurchaseDtl', []); $model = $api->create($data, $model); if (!$model->hasErrors()) { $transaction->commit(); return $this->redirect(['view', 'id' => $model->id]); } else { $transaction->rollBack(); } } catch (\Exception $e) { $transaction->rollBack(); throw $e; } } return $this->render('create', ['model' => $model, 'details' => $model->purchaseDtls, 'greceipt' => $greceipt]); }