简体   繁体   English

如何在symfony2中编辑多个一对多实体?

[英]How do I edit more than one one-to-many entities in symfony2?

Lets imagine I want to create a task app. 假设我要创建一个任务应用程序。

In this app I have users, eg 在这个应用程序中,我有用户,例如

id      name   
1       phil
2       anette

Every user can have x amount of task lists, eg 每个用户可以有x个任务列表,例如

id     user_id    name
1      1          test
2      1          another test
3      2          cheers

Again, every task list can have a list of tasks, eg: 同样,每个任务列表都可以有一个任务列表,例如:

id     tasklist_id    name
1      1              very important task
2      2              important task
3      1              another task in first list
4      3              item for anette

The entity mapping with Doctrine worked out fine. 使用Doctrine进行的实体映射工作得很好。 Also the integration of FOSUserBundle . 还集成了FOSUserBundle I actually can login as an user and can see my very own task lists. 我实际上可以以用户身份登录,并且可以看到自己的任务列表。

But what it not possible for me to create is a way so that a user can edit the tasks in a task list. 但是我无法创建一种方法,以便用户可以编辑任务列表中的任务。 Also it should be noted that only the user which owns a specific task list should be able to edit it and its childs. 还应注意,只有拥有特定任务列表的用户才可以编辑它及其子项。 Via my approach (auto generated crud templates) you can take any id and change every content. 通过我的方法(自动生成的Crud模板),您可以获取任何ID并更改每个内容。

What I thought about to this point: 我现在想到的是:

  • If I supply the task list id to the controller (to show all tasks of a specific list), the id is lost after an update and all tasks from all users will be shown. 如果我将任务列表ID提供给控制器(以显示特定列表的所有任务),则ID在更新后会丢失,并且将显示所有用户的所有任务。 (I created the forms and the controller via the crud generator php app/console generate:doctrine:crud ) (我通过crud generator php app/console generate:doctrine:crud创建了表单和控制器)
  • I thought about putting the task list id into the session, but find this is a bad solution 我考虑过将任务列表ID放入会话中,但是发现这是一个不好的解决方案
  • I tried searching for different approaches (eg symfony2 pass parameter to subentity or symfony2 pass controller variable ) but could not find any information to this topic other than changing the controller into a service 我尝试搜索其他方法(例如,将symfony2 pass parameter to subentitysymfony2 pass controller variable ),但是除了将控制器更改为服务之外,找不到任何与此主题相关的信息
  • I should create a service, maybe out of the task list controller or the task controller. 我应该创建一个服务,可能不在任务列表控制器或任务控制器之外。 But I am not sure about this approach. 但是我不确定这种方法。

Which are correct ways regarding symfony2 to solve this (almost certainly common) problem? 关于symfony2解决此(几乎可以肯定是常见的)问题的正确方法有哪些? How can I supply a user with a way to edit his tasks lists, but also to edit specific tasks? 如何为用户提供编辑其任务列表以及编辑特定任务的方法? Or to be more generic, how to edit more than one one-to-many relationships in symfony? 或更笼统地说,如何在symfony中编辑多个一对多关系?

You should take a look at How to Embed a Collection of Forms 您应该看看如何嵌入表单集合

Concerning your security problem, this is a different matter, but you should check in your controller (in EditAction) that the list you're trying to edit belongs to the logged in user. 关于安全性问题,这是另一回事,但是您应该在控制器中(在EditAction中)检查要尝试编辑的列表是否属于登录用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM