public function setTags(\Model\Card $c, array $itags) { //remove all empty tags, create 2 arrays //containing trimmed tag and lower-case trimmed tag $ltags = array(); $tags = array(); foreach ($itags as $t) { $t = trim($t); if ($t != "") { $ltags[] = strtolower($t); $tags[] = $t; } } $this->em->beginTransaction(); //find all tags, which have to be removed $toRemove = array(); foreach ($c->getTags() as $t) { if (!in_array(strtolower($t->getName()), $ltags)) { $toRemove[] = $t; } } foreach ($toRemove as $t) { $c->removeTag($t); } //find all tags which have to be added $newtags = array(); foreach ($tags as $t) { foreach ($c->getTags() as $tt) { if (strtolower($tt->getName()) == strtolower($t)) { continue 2; } } //not found $newtags[] = $t; } $tm = new TagsManager(); foreach ($newtags as $t) { //try to find existing tag $tag = $tm->findByName(strtolower($t)); if ($tag == null) { $tag = $tm->createTag($t, "#00ff00"); } $c->addTag($tag); } $this->em->flush(); $this->em->commit(); }