您需要再次查看您的实体关系图。经理也是雇员。您实际上只需要两个主要模型:Employee 和 Department。最好使用 id 列来识别记录,因为您可能希望更改部门的 dep_no,这需要更新该部门所有员工的 dep_no。
员工模型
<?php
class Employee extends AppModel {
public $name = 'Employee';
/**
* Model Schema
*
* @var array
* @access protected
*/
protected $_schema = array(
'id' => array('type' => 'integer', 'length' => 8, 'key' => 'primary'),
'first_name' => array('type' => 'string', 'null' => false),
'last_name' => array('type' => 'string', 'null' => false),
'birth_date' => array('type' => 'datetime', 'null' => false),
'gender' => array('type' => 'string', 'null' => false),
'hire_date' => array('type' => 'datetime', 'null' => false),
'department_id' => array('type' => 'integer', 'length' => 8),
'manager_id' => array('type' => 'integer', 'length' => 8),
'created' => array('type' => 'datetime', 'null' => false),
'modified' => array('type' => 'datetime', 'null' => true, 'default' => null)
);
/**
* Model Associations
*
* @var array
* @access public
*/
public $belongsTo = array(
'Department' => array(
'className' => 'Department',
'foreignKey' => 'department_id',
'dependent' => true
),
);
部门模型
<?php
class Department extends AppModel {
public $name = 'Department';
/**
* Model Schema
*
* @var array
* @access protected
*/
protected $_schema = array(
'id' => array('type' => 'integer', 'length' => 8, 'key' => 'primary'),
'number' => array('type' => 'string', 'length' => 8),
'name' => array('type' => 'string', 'null' => false),
'created' => array('type' => 'datetime', 'null' => false),
'modified' => array('type' => 'datetime', 'null' => true, 'default' => null)
);
/**
* Model Associations
*
* @var array
* @access public
*/
public $hasMany = array(
'Employee' => array(
'className' => 'Employee',
'foreignKey' => 'department_id',
'dependent' => true
)
);
public $hasOne = array(
'DepartmentManager' => array(
'className' => 'Employee',
'foreignKey' => 'department_id',
'conditions' => array('DepartmentManager.manager_id' => null),
'dependent' => true
)
);
}
部门主管
$data = $this->Department->find('first', array(
'conditions' => array('Department.number' => 'd006'),
'contain' => array(
'DepartmentManager', 'Employee',
)
));
输出
Array
(
[Department] => Array
(
[id] => 1
[number] => d006
[name] => Human Resources
[created] => 2014-02-25 00:00:00
[modified] =>
)
[DepartmentManager] => Array
(
[id] => 1
[first_name] => David
[last_name] => Scott
[birth_date] => 2014-02-25
[gender] => M
[hire_date] => 2014-02-25
[department_id] => 1
[manager_id] =>
[created] => 2014-02-25 00:00:00
[modified] =>
)
[Employee] => Array
(
[0] => Array
(
[id] => 1
[first_name] => David
[last_name] => Scott
[birth_date] => 2014-02-25
[gender] => M
[hire_date] => 2014-02-25
[department_id] => 1
[manager_id] =>
[created] => 2014-02-25 00:00:00
[modified] =>
)
[1] => Array
(
[id] => 2
[first_name] => Joe
[last_name] => Bloggs
[birth_date] => 2014-02-25
[gender] => M
[hire_date] => 2014-02-25
[department_id] => 1
[manager_id] => 1
[created] => 2014-02-25 00:00:00
[modified] =>
)
[2] => Array
(
[id] => 3
[first_name] => Jane
[last_name] => Bloggs
[birth_date] => 2014-02-25
[gender] => F
[hire_date] => 2014-02-25
[department_id] => 1
[manager_id] => 1
[created] => 2014-02-25 00:00:00
[modified] =>
)
)
)
希望这有帮助。