Jump to content

set_relation() - Edit from both sides of relationship?


  • Please log in to reply
2 replies to this topic

#1 jjwdesign



  • Members
  • PipPip
  • 12 posts

Posted 27 April 2013 - 02:54 AM

Again, thank you for continuing to support this project through this forum!


I think I'm overlooking something simple. I'm trying to allow someone to edit from both sides of a 1-n relationship (set_relation). For example, the Grocery Crud demo of Employees has a relationship to the Office City. You can easily view a list of all employees, edit an employee and change the Office City from a drop down.


Demo: Employees - Office City





But, if you created a list view of Office Cities (or Offices), edit a Office City... there's no way to edit the related Employees to that Office City. What's the best way to create this multi-select drop down (chosen plugin) field? I assume I would need to write my one model after save callback to save the relationship data properly.


n-n relationship?


I noticed that the n-n relationship allows you to edit from both sides of the relationship; as expected. Should I setup a related table and just use a n-n relationship instead? It's not really a n-n, as an Employee can only belong to one Office City (at least in my issue). Not using the proper relationship bothers me a bit. How can I then prevent an Employee from being added to Multiple Cities? Custom add/edit Validation rules?


Am I missing something?

Thanks, Jeff


#2 Sin



  • Members
  • Pip
  • 1 posts

Posted 06 May 2013 - 10:57 AM

Same problem here i think.


I have projects and project objectives 1-N relation.


adding a objective and specifying the project is easy to set up.


But how can i list and change de objectives when i;m viewing a project.



#3 davidoster


    Grocery CRUD Ninja

  • Advanced Member
  • PipPipPip
  • 1,068 posts
  • LocationAthens, Greece

Posted 07 May 2013 - 08:28 AM

Well as you stated is 1-N relationship.

This stops you from being able to see e.g. for a selected city the employees that have their office to this city.

And the reason for that is simple actually. There isn't a table that holds office_id with employee_id since we just have an employee table and a separate office table.


In order to get what you mention you need to make a new table that has employee_id and office_id and of course auto-update when offices' table changes or employees' table changes in order to have it synced.



rtfm_small.jpg---!!!Please read these guidelines before asking to the forums!!!---



David Oster aka George Pasparakis,

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users