예제 #1
0
파일: LoadTagData.php 프로젝트: nass59/Lab
 /**
  * {@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();
 }
예제 #2
0
 /**
  * @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()]);
     });
 }
예제 #3
0
 /**
  * {@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();
     }
 }