0

I'm asking for your help, because I'm not an advanced mysql user, and I'm not very conscious about every operation that I'm able to do.

The project that I'm working onto is a web-app designed to manage a company. The "admin user" should be able to visualize a organization chart of all the company structure, edit, add employees, etc.

The chart its done, but I'm struggling with the mysql structure. I'm not that expert, in fact, I've never worked with hierarchies, and I'm loosing too much time trying to figure out, probably with a wrong approach.

So, I need some expert tip if it's possibile.

Here is an example on how the ogranic chart structure is

Company organic chart

- There are 3 divisions types(A - B - C) -----> *Orange* 

- - There are 3 head offices, 1,2,3. -----> *Green*

- - - Every office, can have three divisions. in this case, office 1 has got division A,B,C / office 2 has got division A / office 3 as got division A,C. (customer wants it in this way...)

- - - - Every division has got 5 roles for his employees(*PINK*). In division A, we have for example Sales Manager, Manager, Team manager, sen. agent, agent.

In division B they have different names, and roles are 5.

Division C has only got 2 roles.


So, the organic chart takes the data from a very simple database, where the main columns are ID and parentID (others columns are labels).

There should be a SQL query which builds the data source table, building the company structure starting from other tables:

Tables structure


I'm struggling!

Please, I need to write a function which, starting from the tables of employees, offices and divisions, creates the data source table(table4), in order to populate the organic chart. (IF a entry is already present, update data!).

Don't forget that there are other tables called like "division1_Roles", "division2_Roles" where every roleCode has a roleName.

1 - Manager
2 - Supervisor
3 - Agent ... 

In table(3) "Employees" there is another column called "RoleCodeID" which I forgot.


I think that this can be the logic path:

1) Query Divisions table(1) and add them to Chart dataSource (table4) 

2) Query Offices table(2), determine which divisions every office have(*how?*), then save in Chart dataSource table(4) the office record. 
Note: If a office has got 2 or 3 divisions, there should be created multiple records in table(4) for every office. table4.parentID will change, in order to get the right hierarchy (select * table(4) where ?).

3) Query Employees table(3);

4) Get the EmployeeDivisionID and 
IF EmployeeDivisionID == 1 -> query RoleCodes_division1 table to get roleNome
ELSE IF EmployeeDivisionID == > query RoleCodes_division2 table to get roleName 
[...]
THEN save the roleName in a variable ($roleName)

5) Add the employee to the Chart dataSource table(4).

IMPORTANT: In the chart, I can add a new employee(under another employee, or office), or an office under a division. Every "box" in the chart has god an ID, which is the ID field in dataSource table(4). In this way, I cannot determine if the user is clicking on an division, office or employee.

For this reason, maybe I need to save officeID, divisionID for every employee in table(4) . But in this way, the other tables are like duplicates.

I don't know folks, I feel a bit mad. Maybe we can try to find a solution step by step, would be a great help for me.

I'm not the kind of person who asks for code, but what I need is the logic to face this problem, and the SQL functions to join the tables correctly. Something like hierarchical sql queries.

I'm using PHP & MySql.

THANKS!!

funder7
  • 1,622
  • 17
  • 30
  • 1
    Probably a duplicate with http://stackoverflow.com/questions/4048151/what-are-the-options-for-storing-hierarchical-data-in-a-relational-database?rq=1 – Istvan Feb 02 '16 at 11:23
  • 1
    Hmmm I readed all and I'm inclined to think this look like a home work. It is too elaborated and full of details... You have to show some effort to convince that this isn't a home work (convince at least me, that am in doubt. Not that I'm that important, I'm not xD ) You said `I'm struggling with the mysql structure` and you post a picture of your structure already done, So I ask, What really you need ??? Remember, SO isn't a free service code. You have to help US so we can help you. – Jorge Campos Feb 02 '16 at 11:31
  • Have you looked at nested sets? – Strawberry Feb 02 '16 at 11:40
  • @JorgeCampos You are important! – Strawberry Feb 02 '16 at 11:40
  • @Jorge Campos: You're totally right. That's why at the end of the post I've said that I don't want someone to write the code for me, but just put me in the right direction, what to study. My problem is that I don't have a lot of time, and if it's possible, I would like to avoid to loose it making too many tests. I need the "tools", then I will build the house with my hands, eheh ;) – funder7 Feb 02 '16 at 11:41
  • @Strawberry a couple of days ago I've read this [article](http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/)...maybe it's what I'm looking for.. – funder7 Feb 02 '16 at 11:43
  • kkkkkkkk thanks @Strawberry made my day! Being important at StackOverflow. – Jorge Campos Feb 02 '16 at 11:45
  • there will be 3 table, (employee, office, divisions). 1.) Role of employe comes in employee table as a new filed (describe more in comment), it wil be parent of office, and office will be parent of division – Monty Feb 02 '16 at 11:46
  • The main reason I said all of that is because your question is full of PRE-DERTERMINED conditions like: "I need a function that do..." and other things. So if you have a problem, you need to explain the problem and what you have tried as a solution, not say what you have and WHAT YOU NEED, let the guys here decide what would be the best solution you may not need a function to do that at all. (this is just an example of what drive me to think this is a homework) – Jorge Campos Feb 02 '16 at 11:48
  • @JorgeCampos ...English is not my first language, I've tried to explain the problem as best as I could, trying to point out all the variables of the project. Yes, I'm IGNORANT, self-learner, and this is a new challenge for me. It's a complex project, and I've tried to solve the problem by myself.This post is long as f. and I didn't wanted to make it longer, that's why I didn't post my code. Anyway, my approach is totally wrong (in my opinion). I absolutely cannot modify the code or the db structure afterwards, when it will be running on the server.This data is delicate, I have to to be sure. – funder7 Feb 02 '16 at 12:08
  • @Monty thanks! @JorgeCampos ***INSERT INTO `testDataSource` (`OfficeID`,`OfficeName`) SELECT `OfficeID`,`Name` FROM `testOffices`*** ... that's a problem encountered. I needed to do a ***SELECT * FROM `testDataSource` WHERE parentID = '0'***, then do the query above, but I don't know how to join two queries of this type. In order to avoid adding an office if it's already present. Duplicates are a problem. – funder7 Feb 02 '16 at 12:37
  • I'm reading about nested set model, It seems better, but looks like it's not indicated if you are going to do a lot of modifications in your hierarchy, which I need to do! – funder7 Feb 02 '16 at 13:47

0 Answers0