Example #1
0
 /**
  * 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->updated_at = date("Y-m-d h:i:s");
     $tag = new Tag();
     $tmp = array();
     $tags = array();
     if (isset($_POST['Tag'])) {
         $pieces = explode(",", Yii::$app->request->post()['Tag']['name']);
         $dataProvider = Post_tags::getTags($id);
         $count = $dataProvider->getModels();
         for ($i = 0; $i < count($count); $i++) {
             for ($j = 0; $j < count($pieces); $j++) {
                 if ($dataProvider->getModels()[$i]['name'] == $pieces[$j]) {
                     array_push($tmp, $pieces[$j]);
                     $pieces[$j] = "";
                 }
             }
         }
         $dataProvider = Tag::getAllTags();
         $count = $dataProvider->getModels();
         for ($i = 0; $i < count($count); $i++) {
             for ($j = 0; $j < count($pieces); $j++) {
                 if ($dataProvider->getModels()[$i]['name'] == $pieces[$j]) {
                     array_push($tmp, $pieces[$j]);
                     $pieces[$j] = "";
                 }
             }
         }
         $ids = "'" . implode("','", $tmp) . "'";
         $dataProvider2 = Tag::getTagsIn($ids);
         for ($i = 0; $i < count($pieces); $i++) {
             $tag = new Tag();
             $tag->name = $pieces[$i];
             $tag->save();
             array_push($tags, $tag->tag_id);
         }
         for ($i = 0; $i < count($dataProvider2->getModels()); $i++) {
             array_push($tags, $dataProvider2->getModels()[$i]['tag_id']);
         }
     }
     $postTags = Post_tags::getPostTags($id);
     for ($i = 0; $i < count($postTags->getModels()); $i++) {
         for ($j = 0; $j < count($tags); $j++) {
             if ($postTags->getModels()[$i]['tag_id'] == $tags[$j]) {
                 $tags[$j] = "";
             }
         }
     }
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         for ($i = 0; $i < count($tags); $i++) {
             $postTags = new Post_tags();
             $postTags->tag_id = $tags[$i];
             $postTags->post_id = $model->post_id;
             $postTags->save();
         }
         return $this->redirect(['view', 'id' => $model->post_id]);
     } else {
         return $this->render('update', ['model' => $model, 'model' => $model, 'tag' => $tag]);
     }
 }