public function save() { $transaction = \Yii::$app->getDb()->beginTransaction(); try { $customers = new Customers(); $customers->c_name = $this->c_name; $customers->c_phone = $this->c_phone; $customers->save(); $orders = new Orders(); $orders->customer_id = $customers->id; $orders->save(); foreach ($this->products as $product) { $orderProducts = new OrderProducts(); $orderProducts->order_id = $orders->id; $orderProducts->product_id = $product['id']; $orderProducts->product_amount = $product['amount']; $orderProducts->save(); } $transaction->commit(); } catch (\Exception $e) { $transaction->rollBack(); \Yii::error($e->getMessage()); throw $e; } }
/** * Creates a new Orders model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Orders(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } }