5

I am working with Grocery CRUD (PHP library that creates a full functional CRUD) and I have very basic table of users.

id | first_name | last_name

Some of this users are relatives. So I want to have separate relation table that looks like

id | first_users_id | second_user_id | relation_type

How can I add this relation to Grocery CRUD so while I'll be editing user's profile I would select other users that are relatives providing relation type for each of them?

moonvader
  • 19,761
  • 18
  • 67
  • 116

1 Answers1

0

Without much to go on you should be able to accomplish it like this: https://www.grocerycrud.com/examples/set_a_relation_n_n

You can also reference the docs here for using the function https://www.grocerycrud.com/documentation/options_functions/set_relation_n_n

$crud->set_table('user_table');
$crud->set_relation_n_n('relatives', 'user_user_table', 'user_table', 'id', 'id', 'id');
Michael St Clair
  • 5,937
  • 10
  • 49
  • 75
  • Unfortunately it is not working - there is error Unknown column 'user_table.relation_type' in 'field list' – moonvader Nov 19 '17 at 09:56
  • `user_table` needs to match up with what your table is actually named, `user_table` lines up with your first table you mentioned and `user_user_table` matches up with the second table you mentioned – Michael St Clair Nov 20 '17 at 02:20
  • I understand it. but as you see in error in this case relation_type field needs to be in user_table. And I have this field in user_user_table. – moonvader Nov 20 '17 at 03:04
  • ok, now I see drop down where I only see ids of users. It will be great if i will be able to see their names and specify relation type. Also how should I provide information about saving id's to first_users_id and second_user_id ? – moonvader Nov 21 '17 at 04:27
  • if I change last id param in your code to {last_name} {first_name} - I see names. But still have question about saving this data and also providing info about relations. – moonvader Nov 21 '17 at 04:31
  • can you not use `$crud->fields(` to add more info for relations? You will need to provide some code and what you have tried because there is not much to go on here – Michael St Clair Nov 21 '17 at 22:07
  • sorry but I don't understand how. I need to select N number of relatives and specify relation type (string) for each of them. After that I want that if user B is set to be relative to user A - I will see and would be able to edit this info in both user profiles – moonvader Nov 22 '17 at 04:29
  • I don't think it is possible to edit/view the relation_type field. I think grocery crud will only let you create a connection between the two – Michael St Clair Nov 23 '17 at 01:33