tags:

views:

21

answers:

1

I am new to cakePHP and I am trying to figure out how to create the following relationship:

alt text

The employees.manager_id will point to the employees.id of their manager. So, in other words, employee Jason will have a manager of Jill. Jason is employees.id 1 with employees.manager_id 2. Jill is employees.id 2 and employees.manager_id null.

How do I setup the model controller in such a way that when indexing, adding, editing, etc. that the manager_id should be looked up from the same table. For example, when indexing, I would like to see that 'Jill' is the manager of jason rather than the number 2...

+1  A: 

you can define following association:

class Employee extrnds AppModel {
var $belongsTo = array(
            'Parent' => array('className' => 'Employee',
                                'foreignKey' => 'manager_id',
                                'conditions' => '',
                                'fields' => '',
                                'order' => ''
            )
    );
}

Then you can access it by:

$this->Employee->Parent->find('list');
Nik
Thank you so much. Can you tell me then in the index.ctp (view), what would I use to call it? I currently use something like this:<?php echo $host['Employee']['name']; ?>what would I use now to show the name of the manager?
XL
I think I see now. I would use something like this:<?php echo $host['Parent']['name']; ?>Thanks so much for your time and giving back help to someone just learning!!
XL
I think it should be <?php echo $host['Employee']['Parent']['name']; ?>, shouldn't it?
sipiatti
Yes, you're right. That is what worked:<?php echo $employee['Parent']['name']; ?>
XL