/**
  * Remove and insert tags in model referer
  * @param array $tags
  * @param Taggable $model
  */
 protected function refreshTags(array $tags, Taggable $model)
 {
     $model->getTags()->clear();
     $this->addTags($tags, $model);
 }
Ejemplo n.º 2
0
 /**
  * Save Tag Relation and add create and save tags if don't exist
  * @param Taggable $model
  */
 public function saveRelation(Taggable $model)
 {
     if (!$model->getRemove()) {
         if ($model->getTags()->count() > 0) {
             $addedTags = $model->getTags();
             $model_id = $model->getModelId();
             $model_name = $model->getModel();
             $addNewTags = $addedTags;
             $tagsRelationExisted = $this->findRelationByTagName($model->getTags(), $model);
             //Remove existed Tag from collection
             foreach ($tagsRelationExisted as $tagExisted) {
                 if ($addedTags->exists(function ($index, Tag $addedTag) use($tagExisted) {
                     return $addedTag->getName() === $tagExisted->getName();
                 })) {
                     $addNewTags->removeElement($tagExisted);
                 }
             }
             //Save
             foreach ($addNewTags as $tag) {
                 $relation = new TagRelation();
                 $relation->setModel($model_name);
                 $relation->setModelId($model_id);
                 $relation->setTag($tag);
                 $this->em->persist($relation);
             }
             if (count($addNewTags) > 0) {
                 $this->em->flush();
             }
         }
     }
 }