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; } }
public function getOrderProducts() { return $this->hasMany(OrderProducts::className(), ['order_id' => 'id']); }