/** * {@inheritdoc} */ public function load(ObjectManager $manager) { $this->manager = $manager; foreach ($this->getData() as $label) { $tag = new Tag(); $tag->setLabel($label); $this->manager->persist($tag); } $manager->flush(); }
/** * @param FormBuilderInterface $builder * @param array $options */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('label', 'text')->add('slug', 'text'); $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) { $label = $event->getData(); if (!is_string($label)) { throw new TransformationFailedException(); } $tag = $this->manager->getRepository('ApiBundle:Tag')->findOneByLabel($label); if (!$tag) { $tag = new Tag(); $tag->setLabel($label); $this->manager->getManager()->persist($tag); $this->manager->getManager()->flush(); } $event->setData(['label' => $tag->getLabel(), 'slug' => $tag->getSlug()]); }); }
/** * {@inheritdoc} */ public function load(ObjectManager $manager) { $this->manager = $manager; foreach ($this->getData() as $article) { $articleObject = new Article(); $articleObject->setTitle($article['title'])->setDescription($article['description'])->setAuthor($article['author']); if (isset($article['status'])) { $articleObject->setStatus($article['status']); } $tagToAdd = []; if (isset($article['tags'])) { foreach ($article['tags'] as $label) { $tag = new Tag(); $tag->setLabel($label); $this->manager->persist($tag); $tagToAdd[] = ['label' => $tag->getLabel(), 'slug' => $tag->getSlug()]; } $articleObject->setTags($tagToAdd); } $manager->persist($articleObject); $manager->flush(); } }