/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = $this->find(); $query->joinWith(['author']); $dataProvider = new ActiveDataProvider(['query' => $query]); $userClass = Yii::$app->getModule('yes')->userClass; /* uncomment to sort by relations table on respective column $dataProvider->sort->attributes['salesId'] = [ 'asc' => ['{{%sales}}.id' => SORT_ASC], 'desc' => ['{{%sales}}.id' => SORT_DESC], ]; $dataProvider->sort->attributes['catprosId'] = [ 'asc' => ['{{%catpros}}.id' => SORT_ASC], 'desc' => ['{{%catpros}}.id' => SORT_DESC], ];*/ $dataProvider->sort->attributes['authorName'] = ['asc' => [$userClass::tableName() . '.username' => SORT_ASC], 'desc' => [$userClass::tableName() . '.username' => SORT_DESC]]; $dataProvider->sort->attributes['term'] = ['asc' => ['title' => SORT_ASC], 'desc' => ['title' => SORT_DESC]]; if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['status' => $this->status, 'isdel' => $this->isdel, 'isfeatured' => $this->isfeatured]); $params = self::queryNumber([['id', $this->tableName()], ['author_id'], ['price'], ['discount']]); foreach ($params as $p) { $query->andFilterWhere($p); } $params = self::queryString([['title'], ['sku'], ['description'], ['content'], ['data'], ['tags'], ['images']]); foreach ($params as $p) { $query->andFilterWhere($p); } $params = self::queryTime([['time']]); foreach ($params as $p) { $query->andFilterWhere($p); } $query->andFilterWhere(['like', 'lower(' . $userClass::tableName() . '.username)', strtolower($this->authorName)]); if ($this->category || $this->term) { $term = $this->term ? $this->term : $this->category; $cquery = $this->find()->select(["array_agg(" . $this->tableName() . ".id)"])->leftJoin(CatPro::tableName() . " as cp", $this->tableName() . ".id = cp.product_id")->leftJoin(Category::tableName() . " as c", "cp.category_id = c.id"); if ($this->category) { $cquery->andWhere("lower(c.title) = '" . strtolower($term) . "'"); } else { $cquery->andWhere("lower(c.title) like '%" . strtolower($term) . "%' or lower(c.description) like '%" . strtolower($term) . "%'"); } $res = $cquery->scalar(); $res = $res == "" ? "{}" : $res; if ($this->category) { //$query->andFilterWhere(["OR","false",$this->tableName().".id = ANY ('".$res."')"]); $query->andFilterWhere(["OR", "false", "'," . str_replace(["{", "}"], "", $res) . ",' like concat('%,'," . $this->tableName() . ".id,',%') "]); } else { $query->andFilterWhere(["OR", "lower(title) like '%" . strtolower($this->term) . "%'", ["OR", "lower(description) like '%" . strtolower($this->term) . "%'", ["OR", "lower(tags) like '%" . strtolower($this->term) . "%'", ["OR", "lower(sku) like '%" . strtolower($this->term) . "%'", ["OR", "lower(content) like '%" . strtolower($this->term) . "%'", "'," . str_replace(["{", "}"], "", $res) . ",' like concat('%,'," . $this->tableName() . ".id,',%') "]]]]]); } } 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 = $this->findModel($id); $model->tags = !empty($model->tags) ? explode(",", $model->tags) : []; if (Yii::$app->request->post()) { $transaction = Yii::$app->db->beginTransaction(); try { $post = Yii::$app->request->post(); $category = []; $data = []; $images = []; if (isset($post['Product']['category'])) { $category = $post['Product']['category']; } if (isset($post['Product']['data'])) { $data = $post['Product']['data']; $post['Product']['data'] = json_encode($data); } if (isset($post['Product']['images'])) { $images = $post['Product']['images']; foreach ($images as $i => $img) { if (empty($img)) { unset($images[$i]); } } $post['Product']['images'] = json_encode(empty($images) ? null : $images); } if (is_array($post['Product']['tags'])) { $post['Product']['tags'] = implode(",", $post['Product']['tags']); } $model->load($post); $model->images = $model->images == "null" ? null : $model->images; if ($model->save()) { $cs = CatPro::deleteAll("product_id = :id", ["id" => $model->id]); foreach ($category as $d) { //$c = CatPro::find()->where("product_id = :id AND category_id = :aid",["id"=>$model->id,"aid"=>intval($d)])->one(); //if (!$c) //{ $c = new CatPro(); //} $c->product_id = $model->id; $c->category_id = $d; $c->isdel = 0; $c->save(); } $transaction->commit(); return $this->redirect(['view', 'id' => $model->id]); } else { $transaction->rollBack(); } } catch (Exception $e) { $transaction->rollBack(); } } $model->images = json_decode($model->images); return $this->render('update', ['model' => $model]); }
/** * @return \yii\db\ActiveQuery */ public function getCatPros() { return $this->hasMany(CatPro::className(), ['category_id' => 'id']); }
/** * @return \yii\db\ActiveQuery */ public function getCatPro() { return $this->hasMany(CatPro::className(), ['product_id' => 'id'])->where("isdel=0"); }