- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的问题与从这些表格中构建表格有关,因为我坚持使用 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 表单字段,白色 下拉菜单是来自首选项表的选项(意味着它们是固定的/特定的并且是无/多选的),蓝色:可能是可以由用户即时添加。
我将在我理解的 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/
我开始从事一个用 Symfony 2.8 编写的大型项目。将整个项目升级到 SF 3 需要数百小时,现在还不可能。我想到了一个想法,将 symfony/symfony 包解压到它替换的单个包中(com
我在提交表单后使用 FOSUserEvents,但订阅者调用了两次。 这样我的验证码第一次有效第二次无效 这是我的代码 router = $router; $this->request
我有以下路线: blog_show: path: /test/123 defaults: { _controller: TotalcanBravofillBundle:Te
我是测试新手。我想测试我的功能。我已经成功安装了 phpUnit。我在互联网上查看了许多教程。但我无法获得有关测试的正确信息。这是我的功能代码: public function loginAction
我正在尝试重现 facebook batch requests 的行为在他们的图形 api 上运行。 所以我认为最简单的解决方案是在 Controller 上向我的应用程序发出几个请求,例如: pub
在 Symfony Progress Bar documentation有一个超酷酒吧的示例图像。不幸的是,看起来文档的其余部分没有解释如何获得这样的结果。 这是图片,以防您错过: 我怎么才能得到它?
我使用Finder发送假脱机电子邮件,但是自动名称生成器将点放在文件名中,有时它们出现在文件的开头。 查找程序似乎无法获取具有该名称的文件-那些文件被隐藏了……有人经历过这种行为吗?有什么建议如何使用
我正在尝试进行 LDAP 身份验证,我目前遇到此类错误: ServiceNotFoundException: The service "security.firewall.map.context.ma
有没有办法验证和检查集合数组是否为空。我已经尝试过: /** * @Assert\NotBlank() * @Assert\Length( min = 1) */ protected $work
使用Smyfony2和Doctrin2,可以使用以下示例创建数据固定装置:http://symfony.com/doc/current/bundles/DoctrineFixturesBundle/i
我看到在大多数Symfony 2示例中,例如,不存在记录时,Symfony 2会引发异常。我认为这种方法对最终用户不友好。为什么有人更喜欢引发异常而不在Flashbag上添加一些错误消息? 最佳答案
我对项目中的以下服务有疑问: app.security.guardAuthenticatorLoginPassword: class: AppBundle\Security\LoginPa
symfony缓存和登录Docker容器存在问题。 Web服务器从www-data用户和组执行,当我使用docker上安装的php从docker容器中清除symfony缓存时,它从root执行。 因此
我想了解 symfony 中的服务 我已阅读http://symfony.com/doc/2.3/book/service_container.html#creating-configuring-se
因为我对 Symfony 和 Doctrine 还很陌生,所以我有一个可能很愚蠢的问题;-) 有人可以用简单的词语向我解释集合(尤其是实体中的ArrayCollections)吗?它是什么以及何时以及
我收到了这个表格: {{ form_start(form) }} {{ form_end(form) }} 我想检查用户是否登录,我这样做了: {% if is_g
我的网站已准备好部署,我正在尝试将其设置为在线。 一些信息: 主持人是 OVH。 它不允许 SSH,我必须使用 FTP 发送文件。也没有命令行。 我现在希望能够在子目录中设置网站:/www/test(
过去几个月以来,我一直在尝试与symfony合作。昨晚我自动删除了不需要的存储库。之后,我无法使用symfony命令创建新的symfony项目。当我在终端中运行Symfony new Security
In the environnement variable, then in system variable, I edited the path and added在环境变量中,然后在系统变量
我们有一个 Symfony 1.4 应用程序,想升级到 Symfony 4。是否有可能或者我们必须重新编程该应用程序? 我们询问了我们附近的一家软件公司,他们告诉我们必须重新编写应用程序。 最佳答案
我是一名优秀的程序员,十分优秀!