Symfony 2.3.6。嵌套表格

时间:2013-10-24 08:59:04

标签: forms symfony entity

问题类似于this one,但存在巨大差异:我没有固定数量的要添加的元素。

Beneath是表单的预览及其外观。我有用户实体的表单,其中包含不同的应用程序实体,每个应用程序实体都有多个用户组实体

The way my form should look like - nested entities

用户

class User extends BaseUser
{
...

/**
 * @ORM\ManyToMany(targetEntity="Application", inversedBy="users")
 * @ORM\JoinTable(name="users_applications")
 */
protected $applications;

/**
 * @ORM\ManyToMany(targetEntity="UserGroup", inversedBy="users")
 * @ORM\JoinTable(name="users_groups")
 */
protected $user_groups;

应用

class Application
{
...

/**
 * @ORM\ManyToMany(targetEntity="User", mappedBy="applications")
 */
protected $users;

/**
 * @ORM\OneToMany(targetEntity="UserGroup", mappedBy="application")
 */
protected $user_groups;

用户组

class UserGroup
{
...

/**
 * @ORM\ManyToOne(targetEntity="Application", inversedBy="user_groups")
 * @ORM\JoinColumn(name="application_id", referencedColumnName="id")
 */
protected $application;

/**
 * @ORM\ManyToMany(targetEntity="User", mappedBy="user_groups")
 */
protected $users;

UserFormType

class UserFormType extends AbstractType
{
    // Array of applications is generated in the Controller and passed over by the constructor
    private $applications;

public function buildForm(FormBuilderInterface $builder, array $options)
{
    ...

    if ($this->applications && count($this->applications) > 0)
    {
        foreach ($this->applications AS $application)
        {
            $builder->add('applications', 'entity', array
            (
                'class' => 'MyBundle:Application',
                'property' => 'title',
                'query_builder' => function(EntityRepository $er) use ($application)
                {
                    return $er->createQueryBuilder('a')
                        ->where('a.id = :id')
                        ->setParameter('id', $application->getId());
                },
                'expanded' => true,
                'multiple' => true
            ));

            $builder->add('user_groups', 'entity', array
            (
                'class' => 'MyBundle:UserGroup',
                'property' => 'title',
                'query_builder' => function(EntityRepository $er) use ($application)
                {
                    return $er->createQueryBuilder('ug')
                        ->where('ug.application = :application')
                        ->setParameter('application', $application);
                },
                'expanded' => true,
                'multiple' => true
            ));
        }
    }
...

问题我已经设法包含应用用户组实体,但是因为应用实体通过循环添加到formbuilder,实体被覆盖,这样多个应用程序只有一个应用程序被渲染。

1 个答案:

答案 0 :(得分:4)

你确实想要收藏品。集合允许任意数量的元素(符合您的要求)。我认为问题是你应该有一个UserGroupType,以便收藏按你想要的方式工作。

这里或多或少是表单类型的完整示例。命名空间和其他内容被省略。

控制器(请记住,您将User对象传递给表单,不要执行您使用private $applications执行的操作):

...
$user = ...;
$form = $this->createForm(new UserFormType(), $user);

对UserFormType的更改:

class UserFormType extends AbstractType
{
    // Don't have that $applications member here...

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        //...

        // Your user has many UserGroups, so display each one as a UserGroup field (see UserGroupFormType)
        $builder->add('user_groups', 'collection', array(
            'type'   => new UserGroupFormType()  // I would make this type a service (http://symfony.com/doc/current/book/forms.html#defining-your-forms-as-services)
        ));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Namespace\Entity\User'
        ));
    }

    // ... getname, etc.
}

新的UserGroupFormType:

class UserGroupFormType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // Each UserGroup has 1 application and multiple users
        // The entity fields allow you to select these

        $builder->add('application', 'entity', array
        (
            'class' => 'MyBundle:Application',
            'property' => 'title',
            'query_builder' => function(EntityRepository $er) use ($application)
            {
                return $er->createQueryBuilder('a')
                    ->where('a.id = :id')
                    ->setParameter('id', $application->getId());
            },
            'expanded' => true,
            'multiple' => false
        ));

        $builder->add('users', 'entity', array
        (
            'class' => 'MyBundle:User',
            'property' => 'title',
            'query_builder' => function(EntityRepository $er) use ($application)
            {
                return $er->createQueryBuilder('ug')
                    ->where('ug.application = :application')
                    ->setParameter('application', $application);
            },
            'expanded' => true,
            'multiple' => true
        ));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Namespace\Entity\UserGroup'
        ));
    }

    // ... getname, etc.
}

另见http://symfony.com/doc/current/cookbook/form/form_collections.html 这将向您展示如何处理在表单中添加新的UserGroups(如果您需要该功能)。