2011/4/7 Vinícius Arantes <[email protected]>:
> Hey everybody!
> I'm new here! I need a help!
>
> how can I model this case in cake?
> in the database and the model of cake
>
> an employee belongs to many departments
> and a department has many employees.
> and an employee is the department head?
>
> I could not think of a way.
>
> I appreciate the help!
class Employee extends AppModel
{
        public $hasAndBelongsTo = array(
                'Department' => array(
                        'className' => 'Department',
                        'joinTable' => 'departments_employees',
                        'foreignKey' => 'employee_id',
                        'associationForeignKey' => 'department_id',
                        'unique' => true
                )
        );
}

class Department extends AppModel
{
        public $hasAndBelongsTo = array(
                'Employee' => array(
                        'className' => 'Employee',
                        'joinTable' => 'departments_employees',
                        'foreignKey' => 'department_id',
                        'associationForeignKey' => 'employee_id',
                        'unique' => true
                )
        );
        
        public $hasOne = array(
                'Head' => array(
                        'className' => 'Employee',
                        'foreignKey' => 'head_id'
                )
        );
}

Add a column in departments table for head_id using whatever type
you're using for employees.id. This is the foreign key.

In your find() calls, refer to the departmant head as class Head. This
is the alis. The class is still Employee.

-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to