この問題は何日も私の頭を悩ませてきました。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
),
);
}
find('all') を実行すると、データベース内のすべての部門からすべてのフィールドが返されますが、関連するデータはまったく返されません。部門コントローラーで:
$departments = $this->Department->find('all');
$this->set(compact('departments'));
他のどのモデルにも問題がなく、部門に関連するものを含む関連データを返すため、モデルに問題があるように感じます。たとえば、地区に関連するすべての部門を見つけることができます。
前もって感謝します!