예제 #1
0
 /**
  * Adds tags to a post
  */
 public function addTags($tags)
 {
     foreach ($tags as $t) {
         $t = trim($t);
         $tag = Tags::findFirst(array("tag = '{$t}'"));
         if (!$tag) {
             $tag = new Tags();
             $tag->tag = $t;
             $tag->save();
         }
         $postTag = PostTags::findFirst(array("conditions" => "posts_id = ?1 AND tags_id = ?2", "bind" => array(1 => $this->id, 2 => $tag->id)));
         if (!$postTag) {
             $postTag = new PostTags();
             $postTag->posts_id = $this->id;
             $postTag->tags_id = $tag->id;
             $postTag->save();
         }
         unset($tag);
         unset($postTag);
     }
 }
예제 #2
0
파일: Post2tags.php 프로젝트: bigbol/ziiwo
 /**
  * tags 更新操作 parent:tags
  *
  * @param $tags
  * @param $titleId
  * @param $model
  * @param $jumpUri
  */
 protected function _tagsUpdate($tags = '', $title_id = 0, $catalog_id = 0, $redirect)
 {
     $tagsRelation = new Post2tags();
     $tagsRelationArray = $tagsRelation->findAll('title_id=:title_id', array('title_id' => $title_id));
     $tagsArray = array();
     foreach ((array) $tagsRelationArray as $row) {
         $tagsArray[] = $row['tag_name'];
     }
     $titleTags = implode(',', $tagsArray);
     $explodeTags = array_unique(explode(',', str_replace(array(' ', ','), ',', $tags)));
     $tagCount = 0;
     foreach ((array) $explodeTags as $value) {
         $tagCount++;
         if ($tagCount >= 10) {
             unset($explodeTags);
             break;
         }
         $TagsArrayNew[] = $value;
         if (!in_array($value, $tagsArray)) {
             $dao = new PostTags();
             $get_data = $dao->find('tag_name=:tag_name', array('tag_name' => $value));
             if (empty($get_data)) {
                 $dao->catalog_id = $catalog_id;
                 $dao->data_count = 1;
                 $dao->tag_name = $value;
                 $dao->create_time = time();
                 $id = $dao->save();
             } else {
                 $get_data->data_count = $get_data->data_count + 1;
                 $get_data->save();
             }
             //写入关联
             self::_tagsRelation($value, $title_id);
         }
     }
     foreach ($tagsArray as $tagName) {
         if (!in_array($tagName, $TagsArrayNew)) {
             $getTagsCount = Post2tags::model()->count('title_id!=:title_id AND tag_name=:tag_name', array('title_id' => $title_id, 'tag_name' => $tagName));
             if ($getTagsCount) {
                 Yii::app()->db->createCommand("UPDATE {{news_tags}} SET data_count=data_count+1 WHERE tag_name='{$tagName}'")->execute();
             } else {
                 PostTags::model()->deleteAll('tag_name=:tag_name', array('tag_name' => $tagName));
             }
             Post2tags::model()->deleteAll('tag_name=:tag_name AND title_id=:title_id', array('tag_name' => $tagName, 'title_id' => $title_id));
         }
     }
 }