public function actionCreate()
 {
     $model = new Tag();
     if ($model->load(Yii::$app->request->post())) {
         if (isset($_POST['ajax'])) {
             Yii::$app->response->format = Response::FORMAT_JSON;
             return ActiveForm::validate($model);
         }
         $model->author_id = Yii::$app->user->getId();
         if ($model->save() && $model->setActive()) {
             // TODO 改为开关审核
             return $this->message($model->getAttributes(), 'success');
         } else {
             return $this->message($model->getErrors());
         }
     }
     throw new NotFoundHttpException('The requested page does not exist.');
 }
Exemple #2
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Tag::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(['like', 'name', $this->name])->andFilterWhere(['like', 'icon', $this->icon])->andFilterWhere(['like', 'description', $this->description]);
     return $dataProvider;
 }
Exemple #3
0
 public function create()
 {
     $question = new Question();
     $question->setAttributes(['subject' => $this->subject, 'content' => $this->content, 'author_id' => $this->author_id]);
     if (($result = $question->save()) && ($result = $question->setActive())) {
         $tags = Tag::findAll(['name' => is_array($this->tags) ? $this->tags : explode(',', $this->tags)]);
         foreach ($tags as $tag) {
             $tagItem = new TagItem();
             $tagItem->setAttributes(['target_id' => $question->id, 'target_type' => $question::TYPE]);
             $tag->addItem($tagItem);
         }
     }
     return $result ? $question : false;
 }
Exemple #4
0
 /**
  * 获取帖子标签(关联tag_item表)
  * @return mixed
  */
 public function getTags()
 {
     return $this->hasMany(Tag::className(), ['id' => 'tid'])->viaTable(TagItem::tableName(), ['target_id' => 'id'], function ($model) {
         $model->andWhere(['target_type' => self::TYPE]);
     });
 }
 public function down()
 {
     $this->dropTable(Tag::tableName());
     $this->dropTable(TagItem::tableName());
 }
Exemple #6
0
 public function getTag()
 {
     return $this->hasOne(Tag::className(), ['tid' => 'id']);
 }