Symfony2:选中表单中的复选框

时间:2022-10-25 16:38:29

I have 3 entity: Person, Affiliation and PersonAffiliation. The person can be edited with the possibility to add or delete affiliatons.

我有3个实体:Person,Affiliation和PersonAffiliation。可以编辑此人,可以添加或删除附属机构。

In my view, I should have a checkbox for every existing affiliation and the checkbox shuld be checked if the person has allready this affiliation.

在我看来,我应该为每个现有的联盟设置一个复选框,如果该人已经拥有该联盟,则应选中复选框。

I know how to display all affiliations in the view (twig) but I don't know how to check the checkboxes if the person has allready this affiliation.

我知道如何在视图(树枝)中显示所有从属关系,但我不知道如果该人已经具有此关联,如何选中复选框。

Here is how the two tables looks like:

以下是两个表的外观:

Person
    id
    firstname
    lastname
    ...
    affiliations

Affiliation
    id
    affiliation

PersonAffiliation
    id
    person_id
    affiliation_id

Now, my question is: Is there a possibility to use the controller and the view (twig) for doing this or the only possibility is to use Javascript (with JQuery)?

现在,我的问题是:是否有可能使用控制器和视图(twig)来做这个或唯一的可能性是使用Javascript(使用JQuery)?

1 个解决方案

#1


1  

Rather than using a proxy class, arrange your objects like this:

而不是使用代理类,安排您的对象,如下所示:

Person
    id
    ... as before
Affiliation
    id
    .. as before
-- remove PersonAffiliation --

but add this relation to your Person class

但是将此关系添加到Person类

/**
 * @ManyToMany(targetEntity="Affiliation")
 * @JoinTable(name="person_affiliation",
 *      joinColumns={@JoinColumn(name="person_id", referencedColumnName="id")},
 *      inverseJoinColumns={@JoinColumn(name="affiliation_id", referencedColumnName="id")}
 *      )
 */

You could do it all in the form builder like this:

您可以在表单构建器中完成所有操作,如下所示:

    $builder
        ->add('person')
        ->add(
            'affiliation',
            'entity',
            array(
                'class'         => 'AcmeDemoBundle:Affiliation',
                'query_builder' => function($em) { return $em->createQueryBuilder('p')->orderBy('p.id', 'ASC'); },
                'property'    => 'affiliation',
                'multiple'    => true,
                'expanded'    => true,
                'required'    => false
            )
        );

And then just render your form with

然后只需渲染表单

{{ form_widget(form) }}

#1


1  

Rather than using a proxy class, arrange your objects like this:

而不是使用代理类,安排您的对象,如下所示:

Person
    id
    ... as before
Affiliation
    id
    .. as before
-- remove PersonAffiliation --

but add this relation to your Person class

但是将此关系添加到Person类

/**
 * @ManyToMany(targetEntity="Affiliation")
 * @JoinTable(name="person_affiliation",
 *      joinColumns={@JoinColumn(name="person_id", referencedColumnName="id")},
 *      inverseJoinColumns={@JoinColumn(name="affiliation_id", referencedColumnName="id")}
 *      )
 */

You could do it all in the form builder like this:

您可以在表单构建器中完成所有操作,如下所示:

    $builder
        ->add('person')
        ->add(
            'affiliation',
            'entity',
            array(
                'class'         => 'AcmeDemoBundle:Affiliation',
                'query_builder' => function($em) { return $em->createQueryBuilder('p')->orderBy('p.id', 'ASC'); },
                'property'    => 'affiliation',
                'multiple'    => true,
                'expanded'    => true,
                'required'    => false
            )
        );

And then just render your form with

然后只需渲染表单

{{ form_widget(form) }}