/** * Display a product * - Detail info of a product * - Related products * @param string $product_id */ public function actionProduct($product_id = null) { $product = Product::findOne($product_id); if (!$product) { throw new HttpException(404, Yii::t('shop', "The requested product not found!")); } // TODO: Calculate related products $this->render('product', ['product' => $product]); }
/** * Add a product into the Cart * @param integer $id The product ID * @throws NotFoundHttpException * @return \yii\web\Response */ public function actionAdd($id, $quantity = 1) { $model = Product::findOne($id); if ($model) { Yii::$app->cart->put($model, $quantity); Yii::$app->session->addFlash('success', "Successfully update your cart!"); return $this->redirect(Yii::$app->user->returnUrl ? Yii::$app->user->returnUrl : ['view']); } throw new NotFoundHttpException(); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Product::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'price' => $this->price, 'category_id' => $this->category_id, 'created_at' => $this->created_at, 'created_by' => $this->created_by, 'updated_at' => $this->updated_at, 'updated_by' => $this->updated_by]); $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'description', $this->description])->andFilterWhere(['like', 'image', $this->image])->andFilterWhere(['like', 'shop_name', $this->shop_name]); return $dataProvider; }
/** * Finds the Product model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Product the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Product::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * @return \yii\db\ActiveQuery */ public function getProducts() { return $this->hasMany(Product::className(), ['shop_name' => 'name']); }
/** * @return \yii\db\ActiveQuery */ public function getProducts() { return $this->hasMany(Product::className(), ['category_id' => 'id']); }