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();
 }
 /**
  * Adds a new movie to the database
  * @param Request $request
  */
 protected function addMovie(Request $request)
 {
     try {
         // Create a new object holding the posted data
         $movie = new Movie();
         $movie->setMovieName($request->request->get('movieName'));
         $movie->setReleaseYear($request->request->get('releaseYear'));
         $movie->setRating($request->request->get('rating'));
         // Auto generate next id and insert data
         $em = $this->getDoctrine()->getManager();
         $this->changeTableName($em);
         $em->persist($movie);
         $metadata = $em->getClassMetaData(get_class($movie));
         $metadata->setIdGenerator(new \Doctrine\ORM\Id\AssignedGenerator());
         $em->flush();
         $response['success'] = true;
         $response['cause'] = 'Movie ' . $request->request->get('movieName') . ' added successfully!';
         // $response['movie_id'] = $movie_id;
     } catch (Exception $e) {
         $response['success'] = false;
         $response['cause'] = 'Invalid request';
     }
     return $response;
 }