/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = ProductSpecification::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, 'product_id' => $this->product_id]); $query->andFilterWhere(['like', 'item_name', $this->item_name])->andFilterWhere(['like', 'item_val', $this->item_val]); return $dataProvider; }
/** * Updates an existing Product model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { $model_q = new ProductSpecification(); $query = ProductSpecification::find()->indexBy('id'); // where `id` is your primary key $query->andWhere(['=', 'product_id', $id]); $dataProvider = new ActiveDataProvider(['query' => $query]); $models = $dataProvider->getModels(); if (Yii::$app->request->isAjax) { if (isset($_POST['ProductSpecification']) && isset($_POST['ProductSpecification']['cat_create'])) { $model_q = new ProductSpecification(); $model_q->attributes = $_POST['ProductSpecification']; if ($model_q->save()) { $response['files'] = ['ok']; $response['result'] = 'success'; $model_uu = new ProductSpecification(); $query = ProductSpecification::find()->indexBy('id'); // where `id` is your primary key $query->andWhere(['=', 'product_id', $id]); $dataProvider = new ActiveDataProvider(['query' => $query]); $models = $dataProvider->getModels(); $product_specifications = ProductSpecification::find()->where(['product_id' => $id])->all(); $response['preview_cont'] = $this->renderAjax('specification_preview', ['data' => $product_specifications]); $response['specification_list'] = $this->renderAjax('ajax_cont', ['dataProvider' => $dataProvider, 'model_q' => $model_uu]); return json_encode($response); } else { var_dump($_POST['ProductSpecification']); $response['result'] = 'error'; $response['files'] = Html::errorSummary($model_q); return json_encode($response); } } else { if (ProductSpecification::loadMultiple($models, Yii::$app->request->post()) && ProductSpecification::validateMultiple($models)) { $count = 0; foreach ($models as $index => $model_q) { // populate and save records for each model if ($model_q->save()) { $count++; } } Yii::$app->session->setFlash('success', "Processed {$count} records successfully."); $response['files'] = "Processed {$count} records successfully."; $response['result'] = 'success'; return json_encode($response); } else { if (isset($_POST['is_banner'])) { $model_uu = new ProductSpecification(); $query = ProductSpecification::find()->indexBy('id'); // where `id` is your primary key $query->andWhere(['=', 'product_id', $id]); $dataProvider = new ActiveDataProvider(['query' => $query]); $models = $dataProvider->getModels(); $product_specifications = ProductSpecification::find()->where(['product_id' => $id])->all(); $response['preview_cont'] = $this->renderAjax('specification_preview', ['data' => $product_specifications]); $response['specification_list'] = $this->renderAjax('ajax_cont', ['dataProvider' => $dataProvider, 'model_q' => $model_uu]); return json_encode($response); } } } } $model = $this->findModel($id); $ProductCategoryRel_model_q = ProductCategoryRel::find()->where(['product_id' => $id])->all(); $ProductImageRel = new ProductImageRel(); if (empty($ProductCategoryRel_model_q)) { $ProductCategoryRel = new ProductCategoryRel(); } else { $ProductCategoryRel = new ProductCategoryRel(); $ProductCategoryRel->category_id = ArrayHelper::getColumn($ProductCategoryRel_model_q, 'category_id'); } if ($model->load(Yii::$app->request->post()) && $ProductCategoryRel->load(Yii::$app->request->post())) { if ($model->save()) { $ProductCategoryRel->product_id = $model->id; if (empty($ProductCategoryRel->category_id)) { ProductCategoryRel::deleteAll(['product_id' => $ProductCategoryRel->product_id]); $ProductCategoryRel->category_id = 0; $ProductCategoryRel->save(); } else { Product::update_product_category_rel($ProductCategoryRel); } return $this->redirect(['view', 'id' => $model->id]); } else { var_dump($model->getErrors()); } } else { return $this->render('update', ['model' => $model, 'ProductCategoryRel' => $ProductCategoryRel, 'ProductImageRel' => $ProductImageRel, 'dataProvider' => $dataProvider, 'model_q' => $model_q]); } }