假设我有3个型号。
职位(在表格申请人中我只存储position_id,这是描述的地方)
class Dentist扩展AppModel { public $ hasOne ='申请人'; }
类申请人扩展AppModel { public $ belongsTo = array('Dentists','Position'); }
类位置扩展AppModel { }
当我在DentistsController中使用$this->Dentist->find('all');
时,我在牙医的视图中遇到了问题,因为SQL就像
select *
from dentists left outer join applicants
on dentists.id = applicants.dentist_id
不再像左外连接位置......
但是如果我在ApplicantsController中使用$this->Applicant->find('all');
,我就会离开外连接位置......
如何设置模型assosiation以从我的DentistsController获取连接语句到表“position”。
谢谢大家。
答案 0 :(得分:0)
Your models should have following association
Positions model:
/**
* hasMany associations
*
* @var array
*/
public $hasMany = array(
'Applicant' => array(
'className' => 'Applicant',
'foreignKey' => 'position_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
);
Application model:
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'Dentist' => array(
'className' => 'Dentist',
'foreignKey' => 'dentist_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
);
/**
* hasMany associations
*
* @var array
*/
public $hasMany = array(
'Dentist' => array(
'className' => 'Dentist',
'foreignKey' => 'application_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
);
Dentist model:
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'Applicant' => array(
'className' => 'Applicant',
'foreignKey' => 'applicant_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
);
/**
* hasMany associations
*
* @var array
*/
public $hasMany = array(
'Applicant' => array(
'className' => 'Applicant',
'foreignKey' => 'dentist_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
);