【发布时间】:2016-08-26 02:08:30
【问题描述】:
我的问题与从这些表中构建表单有关,因为我一直坚持使用 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 类。我能够创建表单,但只是部分,所以帮助不大。我想从 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 */
}
...
}
...
}
同样在 Edit 控制器内部:
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 表的想法。下面附上图片表示的想法。 Green 是用户的原生表单字段,White 下拉列表是 Preferences 表中的选项(意味着它们是固定的/特定的并且是无/多选的),蓝色:可能是可以的由用户即时添加。
然后,我将在 User 和 Preference 之间建立我理解的 ManyToMany 关系。你能告诉我如何从控制器内部实现这个目标吗?
【问题讨论】:
-
您能否更详细地解释一下最终表单的外观?现在我不清楚你想要做什么的描述。你也可以提供你尝试过的东西并告诉它有什么问题。
-
@dragoste:感谢您的评论。我现在添加了更多细节。我想为选定的用户和首选项对将选定的选项保存到 User2Pref 表中。有什么建议吗?
-
好的,我现在看到你的问题了。您在
User中有一个Preferences集合,但您希望通过Preference::group将其拆分为几个表单字段。这是正确的吗? -
没错,我希望能够像我在问题中提到的那样将它们保存到 User2Pref 表中。你能告诉我怎么做吗? :)
标签: doctrine-orm symfony symfony-forms