Example #1
0
 public function load(ObjectManager $manager)
 {
     $faker = Factory::create();
     $wordsExist = array();
     for ($i = 1; $i <= 50; $i++) {
         $tag = new Tag();
         do {
             $tag->setTitle($faker->word);
         } while (in_array($tag->getTitle(), $wordsExist));
         $wordsExist[] = $tag->getTitle();
         $slug = $this->container->get('app.slugger')->slugify($tag->getTitle());
         $tag->setSlug($slug);
         $this->setReference("tag {$i}", $tag);
         $manager->persist($tag);
     }
     $manager->flush();
 }
 /**
  * Extract tags in an Article
  * @param LifecycleEventArgs $args
  */
 public function postPersist(LifecycleEventArgs $args)
 {
     $entity = $args->getEntity();
     $em = $args->getEntityManager();
     if (!$entity instanceof Article) {
         return;
     }
     $length = $this->setLimitFrequency($entity->getContent());
     $tags = $this->extractTags($entity->getContent());
     // Add Tags to an Article
     foreach ($tags as $key => $value) {
         // Check the frequency of the a given tag
         if ($value >= $length) {
             $tag = new Tag();
             $tag->setSlug($key);
             $tag->setFrequency($value);
             $tag->addArticle($entity);
             $entity->addTag($tag);
             $em->persist($tag);
             $em->persist($entity);
         }
     }
     $em->flush();
 }