/** * Updates an existing Post 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()) { $post = Yii::$app->request->post(); $category = []; if (isset($post['Post']['category'])) { $category = $post['Post']['category']; } if (is_array($post['Post']['tags'])) { $post['Post']['tags'] = implode(",", $post['Post']['tags']); } $model->load($post); $transaction = Yii::$app->db->beginTransaction(); try { if ($model->save()) { $cs = BlogCatPos::deleteAll("post_id = :id", ["id" => $model->id]); foreach ($category as $d) { //$c = BlogCatPos::find()->where("post_id = :id AND category_id = :aid",["id"=>$model->id,"aid"=>intval($d)])->one(); //if (!$c) //{ $c = new BlogCatPos(); //} $c->post_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(); } } return $this->render('update', ['model' => $model]); }
/** * @return \yii\db\ActiveQuery */ public function getBlogCatPos() { return $this->hasMany(BlogCatPos::className(), ['post_id' => 'id'])->where("isdel=0"); }
/** * @return \yii\db\ActiveQuery */ public function getBlogCatPos() { return $this->hasMany(BlogCatPos::className(), ['category_id' => 'id']); }
/** * 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('blog')->userClass; /* uncomment to sort by relations table on respective column $dataProvider->sort->attributes['blogcatposId'] = [ 'asc' => ['{{%blogcatpos}}.id' => SORT_ASC], 'desc' => ['{{%blogcatpos}}.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']]); foreach ($params as $p) { $query->andFilterWhere($p); } $params = self::queryString([['title'], ['description'], ['content'], ['tags'], ['image']]); 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; $dsn = $this->db->dsn; $cquery = $this->find(); if (strtolower(substr($dsn, 0, 5)) == "mysql") { $cquery->select(["GROUP_CONCAT(" . $this->tableName() . ".id)"]); } else { $cquery->select(["array_agg(" . $this->tableName() . ".id)"]); } $cquery->leftJoin(BlogCatPos::tableName() . " as cp", $this->tableName() . ".id = cp.post_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(content) like '%" . strtolower($this->term) . "%'", "'," . str_replace(["{", "}"], "", $res) . ",' like concat('%,'," . $this->tableName() . ".id,',%') "]]]]); } } return $dataProvider; }