public function load(ObjectManager $manager)
 {
     $t1 = new Type();
     $t1->setName('Feature Film');
     $t2 = new Type();
     $t2->setName('Documentary');
     $g1 = new Genre();
     $g1->setName('thriller');
     $g2 = new Genre();
     $g2->setName('fantasy');
     $g3 = new Genre();
     $g3->setName('drama');
     $m1 = new Movie();
     $m1->setTitle('Elephant man');
     $m1->setType($t1);
     $m1->getGenres()->add($g2);
     $manager->persist($m1);
     $m2 = new Movie();
     $m2->setTitle('Ice age');
     $m2->setYear(2002);
     $m2->setReleaseDate(new \DateTime('2012-03-12'));
     $m2->setType($t2);
     $m2->getGenres()->add($g1);
     $m2->getGenres()->add($g3);
     $manager->persist($m2);
     $m3 = new Movie();
     $m3->setTitle('Deadpool');
     $m3->setType($t1);
     $m3->getGenres()->add($g1);
     $manager->persist($m3);
     $manager->flush();
 }
 public function importArray(array $data)
 {
     // Turning off doctrine default logs queries for saving memory
     $this->em->getConnection()->getConfiguration()->setSQLLogger(null);
     // Process items 20 by 20
     $batchSize = 100;
     $i = 1;
     // Processing on each row of data
     foreach ($data as $row) {
         // $movieImporter->importCSVRow();
         $movie = $this->em->getRepository('AppBundle:Movie')->findOneBy(array('title' => $row['Title']));
         // If the movie does not exist we create one
         if (!is_object($movie)) {
             $movie = new Movie();
             $movie->setTitle($row['Title']);
         }
         // Updating info
         $movie->setYear($row['Year']);
         $movie->setRating($row['IMDb Rating']);
         $type = $this->em->getRepository('AppBundle:Type')->findOneBy(array('name' => $row['Title type']));
         if (!is_object($type)) {
             $type = new Type();
             $type->setName($row['Title type']);
             $this->em->persist($type);
             $this->em->flush();
         }
         $movie->setType($type);
         $genres = explode(', ', $row['Genres']);
         $movie->getGenres()->clear();
         foreach ($genres as $genre) {
             $g = $this->em->getRepository('AppBundle:Genre')->findOneBy(array('name' => $genre));
             if (!is_object($g)) {
                 $g = new Genre();
                 $g->setName($genre);
                 $this->em->persist($g);
                 $this->em->flush();
                 // Important: without flush, object not written in DB, so not found with findOneBy, and we endup with duplicates
             }
             $movie->getGenres()->add($g);
         }
         // TODO handle other fields here
         // Persisting the current object
         $this->em->persist($movie);
         // Each 20 objects persisted we flush everything
         if ($i % $batchSize === 0) {
             $this->em->flush();
             // Detaches all objects from Doctrine for memory save
             $this->em->clear();
             // Notify progress
             $this->ed->dispatch('movie_importer.progress', new GenericEvent('progress', array('advance' => $batchSize)));
         }
         $i++;
     }
     // Flushing and clear data on queue
     $this->em->flush();
     $this->em->clear();
 }
 /**
  * Creates a form to delete a Type entity.
  *
  * @param Type $type The Type entity
  *
  * @return \Symfony\Component\Form\Form The form
  */
 private function createDeleteForm(Type $type)
 {
     return $this->createFormBuilder()->setAction($this->generateUrl('type_delete', array('id' => $type->getId())))->setMethod('DELETE')->getForm();
 }