简体   繁体   中英

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

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. Also the integration of 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.

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. (I created the forms and the controller via the 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
  • 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
  • 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? 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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