/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Order::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'user' => $this->user, 'offer' => $this->offer, 'added' => $this->added]); return $dataProvider; }
public function actionIndex() { /*$curl = new Curl(); $url = Url::toRoute('user/index','http'); $content =$curl->get($url, 0); p($content);*/ /*$goods = new Goods(); $item = [ 'goods_name' => '香蕉', 'goods_price' => '5.5', 'goods_unit' => '1', 'owner_id' => 1 ]; $rs = $goods->create([$goods->formName()=>$item]); p($rs);*/ /* $item = Goods::findOne(7); $item->goods_price = 9.8; $item->scenario = Goods::SCENARIO_UPDATE; $rs = $item->save(); p($rs); p($item);*/ /*$order = new Order(); $rs = $order->placeOrder([7=>1, 10=>2, 11=>5]); p($rs);*/ $order = Order::findOne(21); $items = $order->getGoods()->all(); // p($items); $orderItem = $order->getOrderItems()->all(); foreach ($orderItem as $item) { $goods = $item->getGoods()->one(); echo $item->item_name . '=>' . date('Y-m-d H:i:s', $goods->created_at) . '价格:' . $goods->goods_price . ' 数量:' . $item->item_number . '总额:' . $item->subtotal . '<br/>'; } /*$orderItem = new OrderItem(); $rs = $orderItem->addOne(7,1,12); p($rs); p($orderItem->getErrors());*/ }
/** * 下单 * @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(); } }
/** * Finds the Order model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Order the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Order::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }