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 data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Orders::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, 'price' => $this->price, 'date_order' => $this->date_order]); $query->andFilterWhere(['like', 'delivery', $this->delivery])->andFilterWhere(['like', 'product_ids', $this->product_ids])->andFilterWhere(['like', 'email', $this->email])->andFilterWhere(['like', 'adress', $this->adress])->andFilterWhere(['like', 'notice', $this->notice])->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'phone', $this->phone]); return $dataProvider; }
public function actionOrders() { $query = Orders::find()->joinWith(['customer', 'orderProducts.product.images', 'orderProducts.product.characteristics']); $model = $query->all(); return $this->render('orders', ['model' => $model]); }
/** * Finds the Orders model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param string $id * @return Orders the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Orders::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }