/** * Transforms a string (text) to an object (searchTag). * * @param string $text * * @return SearchTag|null * * @throws TransformationFailedException if object (searchTag) is not found. */ public function reverseTransform($text) { $text = trim($text); $texts = explode(',', $text); // О нет, надо еще предусмотреть кавычки $tagsStr = array(); foreach ($texts as $key => $value) { $value = trim($value); if (!empty($value)) { $tagsStr[] = $value; } } if (count($tagsStr) == 0) { return new ArrayCollection(); } $searchTags = $this->om->getRepository('EvrikaMainBundle:SearchTag')->findSubset($tagsStr); // Убираем из массива текстов все, которые нашлись в базе foreach ($searchTags as $k1 => $searchTag) { $keys = array_keys($tagsStr, $searchTag->getText()); foreach ($keys as $key) { unset($tagsStr[$key]); } } // Остались те, которых в базе нет, поэтому создаем их foreach ($tagsStr as $tag) { $searchTag = new SearchTag(); $searchTag->setText($tag); $searchTags->add($searchTag); $this->om->persist($searchTag); } return $searchTags; }
public function reverseTransform($text) { $text = trim($text); $text = trim($text, ';'); if (empty($text)) { return null; } $tags = explode(';', $text); $tagIds = array(); if (empty($tags)) { return null; } foreach ($tags as $tagText) { $tagText = trim($tagText); $tag = $this->om->getRepository('EvrikaMainBundle:SearchTag')->findOneByText($tagText); if (empty($tag)) { $tag = new SearchTag(); $tag->setText($tagText); $this->om->persist($tag); $this->om->flush($tag); $this->om->refresh($tag); } $tagIds[] = $tag->getId(); } $kv = $this->om->getRepository('EvrikaMainBundle:KeyValue')->get('tagIds'); $kv->setV(implode(';', $tagIds)); $this->om->flush(); return null; }