Revision #2 has been created by sefburhan on Jan 28, 2014, 10:46:16 AM with the memo:
Tags manipulation using Select2 in single field
« previous (#1) next (#3) »
Changes
Title
unchanged
Tags manipulation using Select2 in single field
Category
unchanged
Tutorials
Yii version
unchanged
Tags
unchanged
select2, tags, tags-delete, tags-manipulation, tags code, multiple tags
Content
changed
[...]
```php
// check if Tags extis
if (isset($_POST['Tags'])) {
// Tags exits
$tagsList = explode(',', $_POST['Tags']);
// get the array of tags
$currentTagsList = array(); //
track to have currently selected tags used for deleted
foreach ($tagsList as $TagValue) {
// check if exist in tag table or notcurrent tags
$previousTagsList2 = Feedback::getAssignedTagsIDs($feedbackModel->id);
foreach ($tagsList as $TagValue) { // added to list
$isExists = Tags::model()->find('name=:name', array(':name' => $TagValue));
if (count($isExists) > 0) {
//
if exists just assign
$tag = new FeedbackTags;
$tag->feedback_id = $feedback_id;
$tag->tag_id = update currentTagList
$currentTagsList[]=$isExists->id;
$tag->save();} else {
$currentTagsList[]=$tag->tag_id; // add to array for track
} else { // if not exist add to tag table and then to tags// add new and update currentTagList
$tagNew = new Tags;
$tagNew->name = $TagValue;
if ($tagNew->save()) {
// now add to feedbacktags table$currentTagsList[]=$tagNew->id;
}
$tag2 = new FeedbackTags;
}
$tag2->feedback_id = $feedback_id;
$tag2->tag_id = $tagNew->id;
$tag2->save();
$currentTagsList[]=$tag->tag_id }
$deletedList=array_diff($previousTagsList2, $currentTagsList); // deleted list
$addedList=array_diff($currentTagsList, $previousTagsList2);
// add
to array for track
}
ed list
foreach ($addedList as $add) { // added new items
}
$tag }
}
$previousTagsList = Feedback::getAssignedTagsIDs($feedbackModel->id); // get all exist in feedbackTags table
$deletedList=array_diff($previousTagsList, $currentTagsList); // difference
if(count($deletedList)){
foreach ($deletedList as $item){
// just delete form feedbackTags table NOT from Tags , if you want you can delete
$tag2->feedback_id = $feedback_id;
$tag2->tag_id = $add;
$tag2->save();
}
foreach ($deletedList as $deleted) { // deleted list if exits
FeedbackTags::model()->find('tag_id='.$
idelete
md .' AND feedback_id='.$feedbackModel->id)->delete();
}
}
}
```[...]