3

这个问题类似于这个问题,但有一个巨大的不同:我没有要添加的固定数量的元素。

下面是表单的预览以及它的外观。我有一个用户实体的表单,其中包括不同的应用程序实体,每个应用程序实体都有多个用户组实体

我的表单应该是这样的 - 嵌套实体

用户

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;

用户窗体类型

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
            ));
        }
    }
...

问题:我已经设法包含了应用程序用户组实体,但是由于应用程序实体是通过循环添加到表单构建器的,因此实体被覆盖,这样多个应用程序只会呈现一个应用程序。

4

1 回答 1

4

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

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

控制器(请记住,您正在将User对象传递给表单,不要做您正在做的事情private $applications):

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

对用户窗体类型的更改:

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.
}

新用户组表单类型:

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 这向您展示了如何处理在表单中添加新的用户组(如果您想要该功能)。

于 2013-11-01T17:18:57.340 回答