I am currently working on a project and must solve the following problem:
One user can be in a group. A group can have one parent group. (Recursively -> the parent group can also have one parent group but several child groups and so on)
A group can have rights and settings which should be inherited to the children. The children (groups and users) can override the different rights / settings.
The settings and rights are only for starters, later more data will be added (prices for products ... etc.)
Rights and settings will be stored in other tables.
No tables have yet been created, this is just the task.
Which is the best way to retrieve for one user the rights / settings (or later any other information)?
Thanks for your ideas and sorry for my english :-)
This is classic parent / child hierarchy. Requires only two fields in a table: One that represents the current ID, and one that represents the parents.
The parent ID points, of course, to the parent. Any records that point to that same parent ID are children. A child record, can also be a parent by another record pointing to the child. When the record is the top-level record, meaning, no more parents, the parent ID is 0.
Example of two "trees" starting with parents (id 1 and id 5 are parents, and the rest are children or grandchildren of those)
id | parent_id | description 1 | 0 | Top-level parent 2 | 1 | Child of 1 (1 -> 2) 3 | 1 | Another child of 1 4 | 3 | Child of 3, grandchild of 1 (1 -> 3 -> 4) 5 | 0 | Another parent 6 | 1 | Yet another child of 1 7 | 5 | Child of 5 (5 -> 7) 8 | 3 | Another child of 3, grandchild of 1 (1 -> 3 -> 8)Permissions would be for each ID. You could derive the "level" in the hierarchy as you read it, or you could store the level. If you stored it, it might look something like this:
id | parent_id | level | description 1 | 0 | 0 | Top-level parent 2 | 1 | 1 | Child of 1 (1 -> 2) 3 | 1 | 1 | Another child of 1 4 | 3 | 2 | Child of 3, grandchild of 1 (1 -> 3 -> 4) 5 | 0 | 0 | Another parent 6 | 1 | 1 | Yet another child of 1 7 | 5 | 1 | Child of 5 (5 -> 7) 8 | 3 | 2 | Another child of 3, grandchild of 1 (1 -> 3 -> 8)Levels might be good for sequencing so that you get the top-level permissions first, then the next, then the next until there are no more. But, managing levels can be a bit of a pain. I've seen this both ways in various implementations.
I recommend looking up parent/child hierarchy for PHP to find some decent examples. Keep in mind, this is a recursive thing.
I haven't dealt with this in awhile, so here's a couple of links that might help. I don't know how useful they'll be for your situation.
stackoverflow.com/questions/13877656/php-hierarch…
stackoverflow.com/questions/2352689/php-recursive…