0

我有一个 'patient_cases' 表,它具有与 'procedures' 表的 HABTM 关系。此“程序”表与“顾问”表具有多对一关系,然后与“专业”表具有多对一关系。

关系工作正常,使用 cakephp 调试工具栏,当我查看“patient_cases”索引页面时,我可以看到一切正常,但是我只得到了consultant_id 和speciality_id,而不是所有具有该关联模型的字段。

我相信这是应该发生的-

在 CakePHP 中,一些关联(belongsTo 和 hasOne)执行自动连接以检索数据,因此您可以发出查询以根据相关关联中的数据检索模型。

但是 hasMany 和 hasAndBelongsToMany 关联并非如此。

我希望能够使用procedures.consultant_id从'consultants'表中获取顾问名称,并使用consultant.specialty_id从'specialties'中获取专业名称。

我试过玩一些表连接但没有成功,

class PatientCase extends AppModel {    
public $hasAndBelongsToMany = array(
                'Procedure'             => array(
                'className'              => 'Procedure',
                'joinTable'              => 'patient_cases_procedures',
                'foreignKey'             => 'patient_case_id',
                'associationForeignKey'  => 'procedure_id',
                'unique'                 => true
            )
    );
}

class Procedure extends AppModel {
    public $belongsTo = array(
            'Consultant'        => array(
                'className'     => 'Consultant'
            )
        );

}

class Consultant extends AppModel {
    public $belongsTo = array(
            'Specialty' => array(
                'className'     => 'Specialty',
                'conditions'    => array('Specialty.active' => 1)
            )
        );
    public $hasMany = array(
            'Procedure'
        );
}
class Specialty extends AppModel {
    public $hasOne = array(
            'Consultant'
        );
    );
}

$this->set('patientCases', $this->PatientCase->find('all'));

返回我需要的所有内容,返回每个模型数组(此处未提及其他关系),但是不返回模型数组“顾问”,这也意味着我无法获取链接到“顾问”模型的“专业”数据

编辑 我已经设法通过在我的 PatientCasesController 中使用它来获取我需要的数据

$this->set('patientCases', $this->PatientCase->find('all'));
$this->set('patientCaseProcedures', $this->PatientCase->Procedure->find('all'));

但是,理想情况下,我想在一个“患者案例”中返回所有内容,这可能吗?

编辑 我已经使用递归函数解决了这个问题

$this->PatientCase->recursive = 3; 
$this->set('patientCases', $this->PatientCase->find('all'));
4

0 回答 0