I’m currently developing a website for a hotel. And one of the things I’m about to implement is worker->superior relationship. What is the best way to do so in MySQL?
Here is what I mean: a chef’s superior is a head chef, head chef’s superior is shift manager, shift manager’s superior is general manager. In the employee table, I could make a field superior with ID of superior employee but then I’m only able to get one superior/upper role; more importantly I wouldn’t be able to retrieve list of all employees that manager manages at the particular hotel.
I’d need advice on this.
Well, with a simple adjacency graph (supervisor_id pointing back at the employees table), you could certainly do what you want, but it won’t be very efficient, and will not scale to large numbers of people.
What you probably want is to implement a nested set model. This allows you to very easily grab everyone who reports to some arbitrary person in the organization.
If you’re early enough in development, you might consider looking at the Doctrine ORM system, which provides a nestedset behavior for models, so you don’t need to implement your own.
Edit: Richard Knop has a post about nested sets with php example code which you might find more helpful than Celko’s 100%-sql examples..