目前我正在使用CakePHP 3.x并有以下用例。
上面提到了5个不同的表,它们的列列在括号中。以下是这些之间的关系。
我在DepartmentsController中定义了以下函数,我的期望是我需要在单个调用中获取以下所有信息。 - 所有部门 - 每个部门的所有指定(如果有的话) - 每个部门指定的所有用户(如果有的话)
img {
width:100%;
height: 100vh;
}
我还在DepartmentsTable.php中定义了以下内容
public function index()
{
$departments = $this->Departments->find('all')
->contain('DepartmentDesignations.Designations')
->contain('Users');
$this->set(compact('departments'));
$this->set('_serialize', ['departments']);
}
我收到以下回复。
$this->belongsToMany('Designations', [
'joinTable' => 'department_designations',
'foreignKey' => 'departments_id',
'targetForeignKey' => 'designations_id'
]);
$this->hasMany('DepartmentDesignations', [
'className' => 'OrganizationManagement.DepartmentDesignations',
'foreignKey' => 'departments_id'
]);
$this->hasMany('DepartmentDesignationUsers',[
'className' => 'OrganizationManagement.DepartmentDesignationUsers',
'foreignKey' => 'department_designations_id'
]);
$this->belongsToMany('Users',[
'joinTable' => 'department_designation_users',
'foreignKey' => 'department_designations_id',
'targetForeignKey' => 'users_id'
]);
我需要department_designations数组中的用户信息。所以在这种情况下我需要做些什么改变。
答案 0 :(得分:0)
根据您的信息,两者之间没有直接关系 部门和用户表,所以你不能使用那种方式为用户。
这样做:
public function index()
{
$departments = $this->Departments->find('all')
->contain('DepartmentDesignations.Designations','DepartmentDesignations.DepartmentDesignationUsers.Users');
$this->set(compact('departments'));
$this->set('_serialize', ['departments']);
}