这是我的数据库架构。 在我的员工控制员中,我想展示属于特定部门的员工 如果dept_no被传递或者显示所有员工。
下面是我的选项数组。目前,它显示所有具有员工部门名称的记录。
$options = array('contain' => array(
'DeptEmp' => array(
'fields' => array('DeptEmp.dept_no')
),
'DeptEmp.Department' => array(
'fields' => array('Department.dept_name')
)
)
);
我的员工模型
$hasMany = array(
'DeptEmp' => array(
'className' => 'DeptEmp',
'foreignKey' => 'emp_no',
'dependent' => false
)
);
我的DeptEmp模型
public $belongsTo=array(
'Employee'=>array(
'className'=>'Employee',
'foreignKey'=>'emp_id',
'dependent'=>false
),
'Department'=>array(
'className'=>'Department',
'foreignKey'=>'dept_no',
'dependent'=>false
)
);
我的部门模特
public $hasMany = array(
'DeptEmp' => array(
'className' => 'DeptEmp',
'foreignKey' => 'dept_no',
'dependent' => false
)
);
我试过
$this->Employee->DeptEmp->dept_no ='d006'
但它没有任何效果。
如果我做错了事,请指导我,因为我是cakephp的新手。
答案 0 :(得分:0)
包含遗憾的是,除了hasOne
关联之外,不会对任何内容进行连接,而是进行多次查询,因此您对所包含数据执行的任何条件都不会过滤原始模型。
但你可以反过来做:找到DeptEmp
为'd006'的dept_no
并包含结果中的所有Employee
。
或者执行联接查找查询,在$options
数组中提供字段joins
。
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#joining-tables
答案 1 :(得分:0)
您需要再次查看实体关系图。经理也是员工。你真的只需要两个主要模型:员工和部门。最好使用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] =>
)
)
)
希望这有用。