Symfony 3.1具有查找表和多个选项的表单保存选项(3个表)

时间:2016-08-26 02:08:31

标签: doctrine-orm symfony symfony-forms

我的问题与这些表中的构建表单有关,因为我坚持使用 Symfony 3.1.1 创建表单。我有这三个表,即User,User2Pref和Preference(我希望他们的关系是自我解释的)。我创建了等效的实体,我也将其包含在下一节中。

======================== 
|         User         | 
|======================| 
| id | Fname  | Lname  |  Profile Table
| 1  | Tom    | Cat    | 
| 2  | Jerry  | Rat    | 
----------------------- 

======================== 
|       User2Pref      | 
|======================| 
| U.id | P.id |  Tag   |  Mapping Table
|   1  |   2  |   a    |  Tag is optional
|   2  |   3  |   b    | 
------------------------ 

========================
|         Pref         | 
|----------------------| 
|  id |  Prop  |  Grp  |  Properties Table
|  1  |  Wht   |  Col  |  Can be grouped, or have more
|  2  |  Blk   |  Col  |  columns (properties)
|  3  |  Abc   |  Xyz  | 
------------------------ 

用户实体:

class User {
    ....
    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\UserPreferenceMapping", mappedBy="user")
     *
     */
     pritected theMappings;
     ....
}

UserToPref实体:

class UserToPref {
    ....
    /**
     * @var \AppBundle\Entity\Preference
     *
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Preference", inversedBy="theMappings")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="preference_id", referencedColumnName="id")
     * })
     */
     private $preference;

    /**
     * @var \AppBundle\Entity\User
     *
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", inversedBy="theMappings")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
     * })
     */
     private $user;

     ....
 }

偏好实体:

class Property {    
    /**
     *
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\UserPreferenceMapping", mappedBy="preference")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="preference_id", referencedColumnName="id")
     * })
     *
     */
    protected $theMappings;
}

对于 formTypes ,我创建了:UserType,UserToPrefType和PropertyType类。我能够创建表单,但只能部分,所以它没有多大帮助。我想从Preference表中提取不同的选项,并能够为[A]保存一个/或多个值。一种新形式,[B]。编辑表格。有人可以帮帮我吗?

已添加详情:#1

======我的表单和控制器======

这是我的UserType表单:(原型)

public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder
        ->add('fname')
        ->add('lname')

        ->add('favColours', ChoiceBoxType:class,

        /* I want to show here all choice of colours from Preferences table and let user choose none/one/multi. 
         * What I tried: fetching them from database directly using query builder, I could show options inside choicebox
        */
        )

        ->add('favHobbies', 
        /* similar as above this time hobbies from Preferences table */
        )

        ->add('moreOptions', 
        /* similar as above with more options from Preferences table */
        )

        /* and so on */
}

在AddNew控制器中,我从:

开始
public function newAction(Request $request) {

    $pref = new Preference();
    $maps = new User2Pref();

    /* NEED HELP TO INIT USER2PREF AND PREFERENCE OBJECTS FOR USER */

    $form = $this->createForm('AppBundle\Form\UserType', $user);
    $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            /* NEED HELP HERE TO PERSIST SUBMITTED DATA INTO User2Pref */
        }
        ...
    }
    ...
}

类似于编辑控制器:

public function editAction(Request $request) {

    $pref = new Preference();
    $maps = new User2Pref();

    /* NEED HELP TO LOAD EXISTING VALUES FROM User2Pref AND PREFERENCE */

    $form = $this->createForm('AppBundle\Form\UserType', $user);
    $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            /* NEED HELP HERE TO PERSIST SUBMITTED DATA INTO User2Pref */
        }
        ...
    }
    ...
}

问题是,addNew表单可以显示选项,但我不知道如何在提交时保留它们。同样也无法加载保存的选项。需要帮忙! :○

详情已添加#2

======打开替代品======

如果更简单地实现要求,我可以放弃User2Pref表的想法。下面附有图形表示的想法。 绿色是用户的原生表单字段,白色下拉列表是Preferences表中的选项(意味着它们是固定/特定的,无/多选),蓝色:可以是可以的东西由用户即时添加。

Requirements

然后我会理解User和Preference之间的ManyToMany关系。你能否告诉我如何从控制器内部实现这一目标?

1 个答案:

答案 0 :(得分:1)

有几种方法可以做到这一点。仅举几例:

  • 自定义PreferenceCollectionType包含处理Preference的字段,必须使用Data TransformerPreference个实体的整个集合转换为几个较小的集合按Preference::grp属性(或其名称)分组。当然,反向变压器反之亦然(将子集合合并为单个集合)。

  • 通过为每个组创建子类,在Preference上使用STI为每个组在User中创建单独的多对多字段。这实际上可能没有STI,但你有责任密切注意数据的凝聚力。

  • 通过在Preference类上实现适当的setter和getter来模拟这些单独的User集合的存在。然后表格会认为有这样的属性。

我不能为您提供现成的解决方案,因为我不知道您需要哪一个,无论如何,如果不运行实际代码,在此处编写它并不容易。

修改 由于您使用的是ManyToOne + oneToMany而不是manytoMany,因此使用STI的第二个解决方案可能难以实现。