⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

set_relation() - Edit from both sides of relationship?



jjwdesign

jjwdesign
  • profile picture
  • Member

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

http://www.grocerycrud.com/documentation/options_functions/set_relation

 

$crud->set_relation('officeCode','offices','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

 


Sin

Sin
  • profile picture
  • Member

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.

 

 


davidoster

davidoster
  • profile picture
  • Member

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.