我遵循数据库架构 -
现在部门,年份和部门表已经填满了信息.
我现在需要插入学生数据.学生数据将从xls文件导入(导入和解析部分已完成).正如您在架构中看到的那样,表中的列student_data
引用year_id
,department_di
和division_id
.因此,在插入时我需要他们的ID字段,因为xls具有各自的名称值.
所以我要根据每个学生的列值来获取相应的ID.因此,这引入了3个查询,用于在学生表中插入一条记录.像这样 -
forloop(...): $studentData = new Entities\StudentData(); $year = $this->em->getRepository("Entities\Year")->findBy(array('year_name' => $this->year[$i])); $department = $this->em->getRepository("Entities\Department")->findBy(array('department_name' => $this->branch[$i])); $division = $this->em->getRepository("Entities\Division")->findBy(array('division_name'=>$this->division[$i])); $studentData->setYear($year[0]); $studentData->setDepartment($department[0]); $studentData->setDivision($division[0]); //other data . . . . . $this->em->persist($studentData); endforloop(); $this->em->flush(); $this->em->clear();
正如你所看到的,我将为每个部门,年份和部门获得ID循环.假设我正在导入100个学生列表,因此它最终会运行300个查询来获取这3个ID字段.
我可以在插入数据时直接从他们的名字中获取年份,部门和部门的ID吗?我是学说的新手,我不知道如何去做.
更新 如果问题不清楚,请告诉我.我可以用更多细节更新它或重组它.