gpt4 book ai didi

doctrine-orm - Symfony 3.1 Forms with lookup table 和 multiple choices save options (3 tables)

转载 作者:行者123 更新时间:2023-12-04 04:34:52 28 4
gpt4 key购买 nike

我的问题与从这些表格中构建表格有关,因为我坚持使用 Symfony 3.1.1 创建表格。我有这 3 个表,即 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 类。我能够创建表单,但只能部分,所以帮助不大。我想从首选项表中提取不同的选项,并能够为 [A] 保存一个/或/多个值。一种新的形式,[B]。编辑表格。谁能帮帮我?

添加了详细信息:#1

====== 我的表单和 Controller ======

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

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 Controller 中,我开始:

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

同样在编辑 Controller 中:

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 表单可以显示选项,但我不知道如何在提交时保留它们。同样也无法加载保存的选项。需要帮忙! :o

添加了#2 的详细信息

====== 开放替代品 ======

如果实现需求更简单,我可以放弃 User2Pref 表的想法。下面附上的是图形表示中的想法。 绿色 是用户的 native 表单字段,白色 下拉菜单是来自首选项表的选项(意味着它们是固定的/特定的并且是无/多选的),蓝色:可能是可以由用户即时添加。

Requirements

我将在我理解的 User 和 Preference 之间建立 ManyToMany 关系。你能给我一些提示,告诉我如何从 Controller 内部实现这个目标吗?

最佳答案

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

  • 自定义 PreferenceCollectionType 包含处理 Preference 的字段,必须使用 Data Transformer这会将 Preference 实体的整个集合转换为按 Preference::grp 属性(或任何其名称)分组的几个较小的集合。当然还有用于反之操作的反向转换器(将子集合合并为一个)。

  • 利用 STI 在用户中为每个组创建单独的多对多字段Preference 通过为每个组创建子类。没有 STI 实际上是可以做到的,但您有责任密切关注数据的凝聚力。

  • 模拟这些单独的 Preference 集合的存在,方法是仅在 User 类上实现适当的 setter 和 getter。那么表单就会认为有这样的属性。

我不会为您提供现成的解决方案,因为我不知道您需要哪一个,而且无论如何,在不运行实际代码的情况下在这里编写它还不够简单。

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

关于doctrine-orm - Symfony 3.1 Forms with lookup table 和 multiple choices save options (3 tables),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39157343/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com