Symfony形成具有许多关系的多个选择实体

时间:2017-08-19 13:58:13

标签: php symfony entity formbuilder

提前抱歉我的英语质量

我会创建一个表单来创建一个Employee。 Employee和Team与ManytoOne双向关联,Team和Division与ManytoOne双向Relation相连,而Division和Center与manytoOne双向Relation相连。在我的表单中,我希望能够使用Javascript过滤团队列表,根据在第一个选择中心然后分区上做出的选择来减少团队列表。

这是我的代码:

EmployeeType

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('birthDate',          DateType::class)
        ->add('firstName',          TextType::class)
        ->add('name',               TextType::class)
        ->add('isActive',           CheckboxType::class, array('required' => false))
        ->add('employeePicture',    EmployeePictureType::class)
        ->add('team',               TeamType::class)
        ->add('save',               SubmitType::class);
}

TeamType

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('shortName',EntityType::class, array(
            'class'         => 'RTELiveWorkingBundle:Team',
            'choice_label'  => 'shortName',
            'placeholder'   => 'Choisir une équipe'
        ))
        ->add('divsion', DivisionType::class);
}

DivisionType

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('shortName',EntityType::class, array(
            'class'         => 'RTELiveWorkingBundle:Division',
            'choice_label'  => 'shortName',
            'placeholder'   => 'Choisir un GMR'
        ))
        ->add('center', CenterType::class);
}

和CenterType

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('shortName',EntityType::class, array(
            'class'         => 'RTELiveWorkingBundle:Center',
            'choice_label'  => 'shortName',
            'placeholder'   => 'Choisir un centre'
        ));
}

我的控制器:

public function addAction(Request $request) {

    $employee = new Employee();

    $form = $this->createForm(EmployeeType::class, $employee);

    if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {

        $em = $this->getDoctrine()->getManager();
        $em->persist($employee);
        $em->flush();

        return $this->redirectToRoute('rte_live_working_employee_view', array('id' => $employee->getId()));
    }

    return $this->render('RTELiveWorkingBundle:Employee:add.html.twig', array('form' => $form->createView()));
}

我的表格:

enter image description here

我有错误:

  

可捕获的致命错误:方法   RTE \ LiveWorkingBundle \ Entity \ Team :: __ toString()必须返回一个字符串   值

但我必须在我的实体中实施__toString()

public function __toString()
{
    return $this->getShortName();
}
你有个主意吗?

0 个答案:

没有答案
相关问题