[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

CakePHP: Modelling Employee Hierarchy

Posted on 2009-04-07
1
Medium Priority
?
1,515 Views
Last Modified: 2013-11-10
Hi All,

How do I model a company's hierarchy? Each employee has zero or more managers and at the same time, might have zero or more employees reporting up to him/her.

Example: CEO --> Directors --> Managers--> Individual Contributors

Is this a HABTM relationship with one table ie. an employee belonging to many employees/having many employees?

Any help or direction would be aprpeciated.

Thanks.
0
Comment
Question by:swfoo
1 Comment
 
LVL 3

Accepted Solution

by:
asianandrew earned 2000 total points
ID: 24094292
You can use a HABTM, but I've always had difficulty with recursion - ex: find all managers who belong to CEO.

If each employee only reports to 1 person (1 employee does not report to more than 1 manager), I'd recommend using a Modified Preorder Tree Transversal (MPTT) schema which would only require 1 table.

MPTT is a little difficult to grasp at first, but can be very powerful. For instance, I could easily find all employees who work for the CEO (Directors, Managers, Individuals), or I could find their first child (Directors) with ease.

Check out the following links:
http://bakery.cakephp.org/articles/view/modified-preorder-tree-traversal-component
http://www.sitepoint.com/article/hierarchical-data-database/

Hope this helps
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month19 days, 15 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question