这个问题困扰了我好几天。当我在我的 Department 模型上执行 find('all') 时,没有获取任何关联的数据。这是我的部门模型:
<?php
App::uses('AppModel', 'Model');
class Department extends AppModel {
public $displayField = 'name';
//The Associations below have been created with all possible keys, those that are not needed can be removed
public $belongsTo = array( ///check
'District' => array(
'className' => 'District',
'foreignKey' => 'district_id'
)
);
public $hasMany = array(
'Group' => array(
'className' => 'Group',
'foreignKey' => 'department_id' ///check
),
'Request'=>array(
'className' => 'Request',
'foreignKey' => 'department_id',
),
'DepartmentPosition'=>array(
'className'=>'DepartmentPosition',
'foreignKey'=>'department_id',
'dependent'=>true
),
);
}
当我找到('all')时,它返回数据库中每个部门的所有字段,但根本没有关联数据。在部门控制器中:
$departments = $this->Department->find('all');
$this->set(compact('departments'));
感觉我的模型有问题,因为我对任何其他模型都没有问题并返回相关数据,包括与部门相关的数据。例如,我可以找到与某个地区相关的所有部门。
提前致谢!