我有以下数据库结构
"subcat_id"
表中的"Course"
列指向"id"
表中的"sub_category"
列。
"instructort_id"
表格中的"Course"
列指向"id"
表格中的"user"
列。
我想在“课程”表中插入数据。我使用Symfony2框架和Doctrine作为数据库库。当我尝试使用以下语句将数据插入课程表时:
$newCourse=new \FrontEndBundle\Entity\Course;
$newCourse->setSubcat($data['subcat']);
$newCourse->setName($data['coursename']);
$newCourse->setInstructor($instructorId);
$newCourse->setDescription($data['description']);
$em->persist($newCourse);
$em->flush();
,我收到错误($newCourse
是课程实体类的对象)
显示的错误如下所示:
我认为该错误与外键问题有关。任何人都可以帮助我如何插入数据 在课程表中正确吗? 在此先感谢.. !!
答案 0 :(得分:0)
这个问题是因为你传递了一个id,它不是SubCategory实体中的setter的SubCategory对象
因此,您必须先检索SubCategory对象,然后将其设置为Course实体
试试这种方式
$subCat = $this->getDoctrine()
->getRepository('FrontEndBundle:SubCategory')
->find($data['subcat']);
然后
$newCourse->setSubcat($subCat);