/** * Creates a new Account model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Account(); $model->scenario = 'insert'; $balance = new Balance(); $balance->scenario = 'insert'; $loaded = $model->load(Yii::$app->request->post()) && ($model->virtual || $balance->load(Yii::$app->request->post())); $valid = false; if ($loaded) { $valid = $model->validate(); $valid = $valid && ($model->virtual || $balance->validate(['sum'])); } if ($valid) { $model->save(); if (!$model->virtual) { $balance->date = date('Y-m-d', strtotime('first day of this month')); $balance->account_id = $model->id; $balance->save(); } return $this->redirect(['view', 'id' => $model->id]); } return $this->render('create', ['model' => $model, 'balance' => $balance]); }