/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = ProductRelation::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(['main' => $this->main, 'sub' => $this->sub]); return $dataProvider; }
/** * create a batch product base on one main product */ public function actionAddBatchProduct($mainID) { $model = new Product(); $model->scenario = Product::SCENARIO_BATCH; $mainProduct = $this->findModel($mainID); $model->attributes = $mainProduct->getAttributes(); $model->stock_qty = null; if ($model->load(Yii::$app->request->post()) && $model->save()) { $relation = new ProductRelation(); $relation->main = $mainID; $relation->sub = $model->id; if ($relation->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { throw new NotFoundHttpException('Error Saving Product'); } } else { $model->qty_per_order = 2; return $this->render('add_batch', ['model' => $model, 'mainID' => $mainID]); } }
/** * @return \yii\db\ActiveQuery */ public function getProductRelations0() { return $this->hasMany(ProductRelation::className(), ['sub' => 'id']); }
/** * Finds the ProductRelation model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $main * @param integer $sub * @return ProductRelation the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($main, $sub) { if (($model = ProductRelation::findOne(['main' => $main, 'sub' => $sub])) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }