dongmu1914 2014-01-15 09:35
浏览 41
已采纳

ManyToMany关系和Symfony2形式:在表单中显示一些选项,保留已保存但未显示的选项

I'm not sure about the title but I'll try to explain my problem. I have a small website to manage semesters, classes, students, attendance etc. Built with Symfony 2.3 and Doctrine 2.

Students are enrolled in some classes that I keep like this in my Student entity:

/**
* @ORM\ManyToMany(targetEntity="Course", inversedBy="students")
*/
private $courses;

The website works like this: you select a semester and then the website will only display information related to this semester (i.e. the schedule, the class reports for the classes of this semester etc.). I keep the selected semester in the session.

So it means that when you want to edit a student profile, to enroll her/him in some classes, the choicelist shows only classes for the selected semester (basically the current one), because otherwise you would have too many choices and they would not be accurate.

I generate this select like this, in the StudentType form:

->add('courses', 'entity', array(
    'class' => 'VirguleMainBundle:Course',
    'query_builder' => $this->doctrine->getRepository('VirguleMainBundle:Course')->getCoursesForSemesterQB($this->semesterId),
    'expanded' => false,
    'multiple' => true,
    'required' => false,
    'attr' => array('class' => 'medium-select')
 ));

But when I submit the form, of course the courses collection is emptied and then filled with the selected choices from the form. It means that enrollement of the student from previous semesters are lost.

I've tried to use a FormEvent, to manually put back the previous saved values, like this:

public function preSubmit(FormEvent $event) {
    $data = $event->getData();
    $form = $event->getForm();

    $currentCourses = $form->getData()->getCourses();
    foreach ($currentCourses as $course) {
        if ($course->getSemester()->getId() != $this->semesterId) {
            $data['courses'][] = $course;
        }
    }
    $event->setData($data);
    $this->customizeForm($form, false);
}

But then I get an "This value is invalid" error, I guess because I'm adding data that were not in the original form, it makes senses.

So I'd like to know basically how I can show for example classes #20, #21 and #22 (linked to semester #2 that we're currently working on), while keeping already saved values #10, #11 and #12 (because they are linked to the semester #1). Or if there is another solution to achieve this, if I completedy failed at my design.

I hope it makes sense and that someone will help me because I'm stuck :)

  • 写回答

1条回答 默认 最新

  • dongsi3826 2015-10-09 15:07
    关注

    The solution was easy: I've reversed the relationship

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 报酬10000,做一个简单的换汇网站
  • ¥15 关于#vue.js#的问题:word excel和ppt预览问题语言-javascript)
  • ¥15 Apache显示系统错误3该如何解决?
  • ¥30 uniapp小程序苹果手机加载gif图片不显示动效?
  • ¥20 js怎么实现跨域问题
  • ¥15 C++dll二次开发,C#调用
  • ¥15 请教,如何使用C#加载本地摄像头进行逐帧推流
  • ¥15 Python easyocr无法顺利执行,如何解决?
  • ¥15 为什么会突然npm err!啊
  • ¥15 java服务连接es读取列表数据,服务连接本地es获取数据时的速度很快,但是换成远端的es就会非常慢,这是为什么呢