public function saveTagging(Taggable $resource)
    {
        $oldTags = $this->getTagging($resource);
        $newTags = $resource->getTags();
        $tagsToAdd = $newTags;

        if ($oldTags !== null and is_array($oldTags) and !empty($oldTags)) {
            $tagsToRemove = array();

            foreach ($oldTags as $oldTag) {
                if ($newTags->exists(function ($index, $newTag) use ($oldTag) {
                    return $newTag->getName() == $oldTag->getName();
                })) {
                    $tagsToAdd->removeElement($oldTag);
                } else {
                    $tagsToRemove[] = $oldTag->getId();
                }
            }

            if (sizeof($tagsToRemove)) {
                $builder = $this->em->createQueryBuilder();
                $builder
                    ->delete($this->taggingClass, 't')
                    ->where('t.tag_id')
                    ->where($builder->expr()->in('t.tag', $tagsToRemove))
                    ->andWhere('t.resourceType = :resourceType')
                    ->setParameter('resourceType', $resource->getTaggableType())
                    ->andWhere('t.resourceId = :resourceId')
                    ->setParameter('resourceId', $resource->getTaggableId())
                    ->getQuery()
                    ->getResult()
                ;
            }
        }

        foreach ($tagsToAdd as $tag) {
            $this->em->persist($tag);
            $this->em->persist($this->createTagging($tag, $resource));
        }

        if (count($tagsToAdd)) {
            $this->em->flush();
        }
    }
public function saveTagging(Taggable $resource)
    {
        $oldTags = $this->getTagging($resource);
        $newTags = $resource->getTags();
        $tagsToAdd = $newTags;

        if ($oldTags !== null and is_array($oldTags) and !empty($oldTags)) {
            $tagsToRemove = array();

            foreach ($oldTags as $oldTag) {
                if ($newTags->exists(function ($index, $newTag) use ($oldTag) {
                    return $newTag->getName() == $oldTag->getName();
                })) {
                    $tagsToAdd->removeElement($oldTag);
                } else {
                    $tagsToRemove[] = $oldTag->getId();
                }
            }

            if (sizeof($tagsToRemove)) {
                $builder = $this->em->createQueryBuilder();
                $builder
                    ->delete($this->taggingClass, 't')
                    ->where('t.tag_id')
                    ->where($builder->expr()->in('t.tag', $tagsToRemove))
                    ->andWhere('t.resourceType = :resourceType')
                    ->setParameter('resourceType', $resource->getTaggableType())
                    ->andWhere('t.resourceId = :resourceId')
                    ->setParameter('resourceId', $resource->getTaggableId())
                    ->getQuery()
                    ->getResult()
                ;
            }
        }

        foreach ($tagsToAdd as $tag) {
            $this->em->persist($tag);
            $this->em->persist($this->createTagging($tag, $resource));
        }

        if (count($tagsToAdd)) {
            $this->em->flush();
        }
    }
public function saveTagging ( Taggable $ resource ) { $ oldTags = $ this -> getTagging ( $ resource ) ; $ newTags = $ resource -> getTags ( ) ; $ tagsToAdd = $ newTags ; if ( $ oldTags !== null and is_array ( $ oldTags ) and ! empty ( $ oldTags ) ) { $ tagsToRemove = array ( ) ; foreach ( $ oldTags as $ oldTag ) { if ( $ newTags -> exists ( function ( $ index , $ newTag ) use ( $ oldTag ) { return $ newTag -> getName ( ) == $ oldTag -> getName ( ) ; } ) ) { $ tagsToAdd -> removeElement ( $ oldTag ) ; } else { $ tagsToRemove [ ] = $ oldTag -> getId ( ) ; } } if ( sizeof ( $ tagsToRemove ) ) { $ builder = $ this -> em -> createQueryBuilder ( ) ; $ builder -> delete ( $ this -> taggingClass , 't' ) -> where ( 't.tag_id' ) -> where ( $ builder -> expr ( ) -> in ( 't.tag' , $ tagsToRemove ) ) -> andWhere ( 't.resourceType = :resourceType' ) -> setParameter ( 'resourceType' , $ resource -> getTaggableType ( ) ) -> andWhere ( 't.resourceId = :resourceId' ) -> setParameter ( 'resourceId' , $ resource -> getTaggableId ( ) ) -> getQuery ( ) -> getResult ( ) ; } } foreach ( $ tagsToAdd as $ tag ) { $ this -> em -> persist ( $ tag ) ; $ this -> em -> persist ( $ this -> createTagging ( $ tag , $ resource ) ) ; } if ( count ( $ tagsToAdd ) ) { $ this -> em -> flush ( ) ; } }
Saves tags for the given taggable resource

@param Taggable  $resource   Taggable resource
Saves tags for the given taggable resource