在symfony 5.3中保留带有密钥复制的循环

pinkon5k  于 2022-11-16  发布在  其他
关注(0)|答案(1)|浏览(101)

我在控制器中有一个带有持久性对象的循环:

$em = $this->getDoctrine()->getManager();
            $courserepository = $em->getRepository(Course::class);
            $courseByRepository = $courserepository->findCourseByFiveParameters($timestamp, $grille, $reunion, $courseNumber, $emailstring);
            
            //persist all cheval
            $num = (int)($request->request->get('loop'));
            $chevalArray = array();
           
            for($i=1; $i<=$num; $i++){
                $cheval = new Cheval;
                $cheval->setTimestamp($timestamp);
                $cheval->setEmail($email);
                $cheval->setGrille($grille);
                $cheval->setReunion($reunion);
                $cheval->setCourseNumber($courseNumber);
                $cheval->setNumero(htmlspecialchars($request->request->get('numero'.$i)));
                $cheval->setNom(htmlspecialchars($request->request->get('nom'.$i)));
                $cheval->setAge(htmlspecialchars($request->request->get('age'.$i)));
                $cheval->setSexe(htmlspecialchars($request->request->get('sexe'.$i)));
                $cheval->setCorde(htmlspecialchars($request->request->get('corde'.$i)));
                $cheval->setOeilleres(htmlspecialchars($request->request->get('oeilleres'.$i)));
                $cheval->setFerrage(htmlspecialchars($request->request->get('ferrage'.$i)));
                $cheval->setNbreCourses(htmlspecialchars($request->request->get('nbreCourses'.$i)));
                $cheval->setVictoires(htmlspecialchars($request->request->get('victoires'.$i)));
                $cheval->setPlaces(htmlspecialchars($request->request->get('places'.$i)));
                $cheval->setGains(htmlspecialchars($request->request->get('gains'.$i)));
                $cheval->setHandPoids(htmlspecialchars($request->request->get('handPoids'.$i)));
                $cheval->setEntraineur(htmlspecialchars($request->request->get('entraineur'.$i)));
                $cheval->setJockey(htmlspecialchars($request->request->get('jockey'.$i)));
                $cheval->setDistance(htmlspecialchars($request->request->get('distance'.$i)));
                $cheval->setMusique(htmlspecialchars($request->request->get('musique'.$i)));
                //ManyToOne
                $course->getId($courseByRepository->getId());
                $cheval->setCourse($course);
                //add to array
                $chevalArray[] = $cheval;
                //persist array[key]
                $manager->persist($chevalArray[$i-1]);
            }
            $manager->flush();

但主键重复:

An exception occurred while executing a query: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '42' for key 'cheval.PRIMARY'

我试图坚持只是:

$cheval = new Cheval;
                $cheval->setTimestamp($timestamp);
                $cheval->setEmail($email);
...
                $manager->persist($cheval);
                $manager->flush;

AND在循环外清除:

}
                $manager->flush;

为什么在我为循环的每一圈重新示例化对象时,键都保留在内存中?!

2uluyalo

2uluyalo1#

好的,我找到了回应,我有一个一对多的taf为我的实体,诺克斯我刚刚切换到ManyToOne &它很好的作品,谢谢大家...

相关问题