public function setTagsAndSave($item,$tags) { $last = NULL; if($del_tags = $item->fetch('BlogTag')) { foreach($del_tags as $del_tag) { $last = $del_tag->fetchSingle('TagDescribesEntry'); $last->deleteLater(); } } if($last) $last->purge(); //ADD TAGS FOR THE ITEM $tag_array = explode(',',$tags); $trimmed = array(); $item = $item->restrict(); $item->read(); foreach($tag_array as $tmp) $trimmed[] = trim($tmp); $tag_array = $trimmed; $tag = new BlogTag(); $tag->clauseSafe('tag_name',$tag_array,Clause::IN); $flipped = array_flip($tag_array); if($existing_tags = $tag->fetch()) { foreach($existing_tags as $existing) { unset($flipped[$existing->toString()]); $item->add($existing); } } $tags = array_flip($flipped); $new_tag = NULL; if(count($tags)) { foreach($tags as $tag_name) { $new_tag = new BlogTag(); $new_tag->set('tag_name',$tag_name); $item->add($new_tag); } } $item->save(); }