public function saveTagging(BaseTaggable $resource) { $tags = clone $resource->getTags(); parent::saveTagging($resource); if (sizeof($tags) !== sizeof($resource->getTags())) { // parent::saveTagging uses getTags by reference and removes elements, so it ends up empty :-/ // this causes all tags to be deleted when an entity is persisted more than once in a request // Restore: $this->replaceTags($tags->toArray(), $resource); } }
/** * Persist product (Add or update) * * @param Product $product * @param $tags * @return Product */ private function persistProduct(Product $product, $tags) { $tags = $this->tagManager->splitTagNames($tags); foreach ($tags as $tag) { $singleTag = $this->tagManager->loadOrCreateTag(trim($tag)); $this->tagManager->addTag($singleTag, $product); } $this->entityManager->persist($product); $this->entityManager->flush(); $this->tagManager->saveTagging($product); return $product; }
/** * {@inheritdoc} * * добавлено инвалидация кеша */ public function saveTagging(Taggable $resource) { $cacheDriver = $this->em->getConfiguration()->getResultCacheImpl(); $oldTags = $this->getTagging($resource); $newTags = $resource->getTags()->toArray(); $allTags = []; if (!is_array($oldTags)) { $oldTags = []; } $tmpTags = array_merge($oldTags, $newTags); foreach ($tmpTags as $tag) { $allTags[] = $tag->getName(); } $allTags = array_unique($allTags); foreach ($allTags as $tag) { $len = mb_strlen($tag, 'UTF8'); for ($i = 1; $i <= $len; $i++) { $str = mb_substr($tag, 0, $i, 'UTF8'); $cacheDriver->delete('tags:findByWord:' . $str); } } parent::saveTagging($resource); }