/** * Creates a new Order model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Order(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } }
/** * 下单 * @param array $goodsAndNumber 商品id和商品数量组合数组 ex: [2=>1,3=>2] id为2的商品数量为1,id为3的商品数量为2 * @return bool */ public function placeOrder(array $goodsAndNumber) { if (!$this->_beforePlaceOrder($goodsAndNumber)) { return false; } $transaction = Order::getDb()->beginTransaction(); try { $order = new Order(); $order->customer_id = $this->_customer_id; $order->order_no = $this->getOrder_no(); if ($order->save() && $this->_afterPlaceOrder($order->order_id, $goodsAndNumber)) { //统计订单金额 $subtotal = OrderItem::find()->where('order_id = :order_id ', [':order_id' => $order->order_id])->sum('subtotal'); //更新订单金额 $orderUp = Order::findOne(['order_id' => $order->order_id]); $orderUp->subtotal = $subtotal; $orderUp->save(); $transaction->commit(); return true; } else { $this->addErrors($order->errors); return false; } } catch (Exception $e) { $transaction->rollBack(); } }