0

我必须创建一个学校经理。当教师必须提交评估结果时,他必须通过评分来评估给定的技能。

Evaluation------OneToMany Scale-----------ManyToOne------Skill

评估类型

class EvaluationType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('scales', CollectionType::class, [
                'entry_type' => ScaleType::class
            ]);
    }
}

比例类型

class ScaleType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('value');
    }
}

我想动态添加ScaleType子元素以EvaluationType::scales形成给定Skills 集合的子元素。

到目前为止,这是我在控制器操作中尝试过的:

public function newEvaluation(Classroom $classroom, Subject $subject, Period $period)
{
    $evaluation = new Evaluation();
    $evaluation->setClassroom($classroom);
    $evaluation->setSubject($subject);
    $evaluation->setPeriod($period);

    $form = $this->createForm(EvaluationType::class, $evaluation);
    $skills =$this->getDoctrine()->getRepository('AppBundle:Skill')->findAll();
    foreach($skills as $skill) {
        $scale = new Scale();
        $scale->setEvaluation($evaluation);
        $scale->setSkill($skill);
        $form['scales']->add($this->createForm(ScaleType::class, $scale, [
            'auto_initialize' => false
        ]));
    }

    return $this->render('classrooms/newEvaluation.html.twig', [
        'form' => $form->createView(),
        'classroom' => $classroom,
        'subject' => $subject
    ]);
}

问题是我在渲染字段中只得到一个子表单scales,看起来当我添加一个子表单时$form['scales']会覆盖以前的孩子。

我想我完全错了。我怎样才能做到这一点?

我还指出了这样一个事实,即我无法创建第Evaluation一个,然后在另一个页面上创建所有Scales。它需要在同一个控制器/页面中完成。

编辑:我将添加一个事实,即我不希望 的子级scales可扩展,它们必须由服务器修复,而不是由客户端获取。

4

1 回答 1

0

解决了,感谢 Artamiel,这是我的新控制器操作:

public function newEvaluation(Classroom $classroom, Subject $subject, Period $period)
{
    $evaluation = new Evaluation();
    $evaluation->setClassroom($classroom);
    $evaluation->setSubject($subject);
    $evaluation->setPeriod($period);

    $skills = $this->getDoctrine()->getRepository('AppBundle:Skill')->findAll();
    foreach ($skills as $skill) {
        $scale = new Scale();
        $scale->setSkill($skill);
        $scale->setEvaluation($evaluation);
    }

    $form = $this->createForm(EvaluationType::class, $evaluation);

    return $this->render('classrooms/newEvaluation.html.twig', [
        'form' => $form->createView(),
        'classroom' => $classroom,
        'subject' => $subject
    ]);
}
于 2016-06-25T21:17:05.887 回答