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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么