php - Multipe flush with doctrine -


i have load users/students list .csv file. each student member of 1 promotion.

in file have users' infos , name of promotion. have insert promotions before inserting students. part works.

protected function execute(inputinterface $input, outputinterface $output) {     /* works */     $start = new \datetime();     $output->writeln('[start] time : ' . $start->format('h:i:s'));     $f = fopen($input->getargument('filename'), 'r');      if ($input->getoption('has-headers'))         fgetcsv($f, null, ',');      $doctrine = $this->getcontainer()->get('doctrine');     $em = $doctrine->getmanager();     $um = $this->getcontainer()->get('fos_user.user_manager');      $promorepo = $em->getrepository('planningbundle:promotion');     $promotions = $promorepo->findall();     /** @var promotion[] $promosidx */     $promosidx = array();      foreach ($promotions $promotion) {         $promosidx[$promotion->getname()] = $promotion;     }      while ($user = fgetcsv($f)) {          if (!array_key_exists($user[7], $promosidx)) {             $promotion = new promotion();             $promotion->setname($user[7])                 ->setdateend(new \datetime())                 ->setdatestart(new \datetime());              $em->persist($promotion);         }     }      $em->flush();     $em->clear();      $doctrine->resetmanager();     $em = $doctrine->getmanager();     /* */     $f = fopen($input->getargument('filename'), 'r');      if ($input->getoption('has-headers'))         fgetcsv($f, null, ',');      $promorepo = $em->getrepository('planningbundle:promotion');      $userrepo = $em->getrepository('planningbundle:user');      $i = 0;     while ($user = fgetcsv($f)) {          $student = new student();         $student             ->setusername($user[0])             ->setemail($user[1])             ->setlastname($user[2])             ->setfirstname($user[3])             ->setpromotion( $promorepo->findonebyname($user[7]) )             ->setpassword('insta' . (new \datetime())->format('y'))         ;          if( is_null($userrepo->findonebyusername($student->getusername())) ) {             $i++;             $um->updateuser($student, false);         }          if (($i % 20) == 0) {             try {                 $em->flush();                 $em->clear();             } catch (\exception $e) {                 $output->writeln('[error] reason : ' . $e->getmessage());             }         }      }      try {         $em->flush();         $em->clear();     } catch (\exception $e) {         $output->writeln('[error] reason : ' . $e->getmessage());     }      $end = new \datetime();     $diff = $end->diff($start);     $output->writeln( "[end] duration : " . $diff->h . ":" .$diff->i .":" .$diff->s );  } 

is correct ? tried many possibilities, , having multiple exceptions ... , 1 did not insert user.

i used :

$em->flush(); $em->clear(); $em = $this->getcontainer()->get('doctrine')->getmanager(); 

in order reload manager. solves problem , allows avoid the entity manager closed exception.


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -