Hi,

I have a table in my database that I access that gives me information about a group such as group name, group purpose etc..., that group has members and I d like to capture the uid of all the groups members and save that, should I save it all as an array in a single field in my database (Never going to be more than about 30 members per group) or should I create a second table and store the information there? What is the recommended way to solve this common problem please.

Sorry if this is a daft question, I am new to coding in D7, loving learning how to write custom modules and just want to make sure I do this right from the start

Many Thanks

Gibbo

Comments

Jaypan’s picture

Create a second table for users. It should have two columns, gid (group ID) and uid (User ID). You'll store these two values in there in order to relate your groups to your users.

gibbo1715’s picture

Many Thanks, does the same principle apply if I want to show multiple groups are associated, i.e. gid(group ID1) and gid2(group ID 2) or is there a better way for this use case

Jaypan’s picture

Same thing. In fact, the case I gave was for a many-to-many (many groups to many users) relationship. If each user was only a member of a single group, you would simply add a column to the user's table for the Group ID. This method allows you to have many users in a group, as well as allows a user to have many groups.

This isn't a Drupal thing by the way, it's how relational databases are structured.