lundi 22 mai 2017

How to organize two actions: create 2 models and link one to another OR create only one model (depending on the user's choice)

i dont know what to do in this situation. I have two models:

  • Comments (task_id, text)
  • Task (title, priority, type, description)

I need to give a user choice on how to create this models:

  • Create a task itself
  • Create a simple comment (task_id = null)
  • Create a comment AS a task (dont ask why, it's a part of my idea :) )

The first two are done, i have separate controllers and methods. But the last one is a bit tricky: When user presses 'create comment as a task'(+ he fills the form with task properties) the server side should create a new task and then create a new comment with task_id link to the created task. All i can imagine is dirty repeated code.

The best way i found:

  • Create an event named 'CreatedTaskFromCommentsSection' and call the comment repo there(in listener) to create a new comment, linked to task
  • then use TaskController->create method and pass additional comment info in request and check if($request->comment_title) then call the event

But i think it's really bad logic. Maybe i should use another table structure for this? Will appreciate any advices here :)

Aucun commentaire:

Enregistrer un commentaire