public function actionIndex() { $model = new OrderForm(); if ($model->load(Yii::$app->request->post()) && $model->makeOrder()) { $model->good_id++; // create a new order $order = new Order(); // set total price $model->total_price = Good::findAll($model->good_id)[0]->price * $model->goods_count; // fill order props foreach ($model as $key => $value) { $order->setAttribute($key, $value); } // save the order $order->save(); return $this->redirect('/index.php?r=order%2Forders'); } return $this->render('index', ['model' => $model, 'good_names' => HelperComponent::array_pluck('name', Good::find()->select('name')->all())]); }
/** * @return \yii\db\ActiveQuery */ public function getGood() { return $this->hasOne(Good::className(), ['id' => 'good_id']); }