2013-10-24 72 views
3

該問題與this one類似,但是存在巨大差異:我沒有固定數量的要添加的元素。Symfony 2.3.6。嵌套形式

下面是窗體的預覽以及窗體的外觀。我有一個用戶實體的表格,其中包括不同的應用實體,每個應用實體有幾個用戶組實體

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循環中,實體被覆蓋,使得多個應用程序只有一個應用程序被渲染。

+2

我們對嵌套表單有一些類似的設置,但我們使用集合,也許它可以成爲您的問題的解決方案! http://symfony.com/doc/current/cookbook/form/form_collections.html – acrobat

+0

您在每次迭代中添加相同的字段(名稱應用程序)。這是不可能的。我建議爲實體子類型創建特殊類型 – zulus

回答

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

New 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 這說明如何處理在表單中添加新的用戶組(如果需要該功能)。

+0

感謝您的努力!你的解決方案中的問題是,我不會得到我想要的分層結構:1. application1; 1.1。 1組; 1.2。第2組; 2. application2等第二件事是我編輯用戶實體,我想選擇應用程序/組 - 在您的解決方案中,我將編輯usergroup實體。同時,我將實體分配分成幾種形式 - 但我會將您的答案標記爲已接受。 – Vilius