/** * @return ActiveQuery */ public function getUserTags() { return $this->hasMany(UserTag::className(), ['tag_id' => 'id']); }
/** * Updates an existing User 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); $contactModel = UserContact::findOne(['user_id' => $model->id]); /** @var UserContact $contactModel */ if ($model->load(Yii::$app->request->post()) && $contactModel->load(Yii::$app->request->post())) { if ($contactModel->save() && $model->save()) { UserTag::updateAll(['status' => 'inactive'], ['user_id' => $model->id]); $tags = Yii::$app->request->post('User')['tags']; array_walk($tags, function (&$arr) use($model) { $userTagModel = new UserTag(); $userTagModel->created_by = Yii::$app->user->id; $userTagModel->tag_id = $arr; $userTagModel->user_id = $model->id; if (!$userTagModel->save()) { throw new Exception("Cannot save user tag!"); } }); } return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('update', ['model' => $model, 'contactModel' => $contactModel]); } }
public static function getCurrentUserTags() { $query = UserTag::find()->select('tag_id')->where(['user_id' => Yii::$app->user->id])->groupBy('tag_id')->all(); $user_tag = []; foreach ($query as $model) { $user_tag[] = $model->tag_id; } //$user_tag = rtrim($user_tag, ','); return $user_tag; }