2013-04-23 62 views
0

假設我有3個模型。CakePHP 2請幫助模型協會

  1. 牙醫
  2. 申請人(每個用戶的更多細節,如地址,電話,interested_position等)
  3. 位置(表申請人本人只存儲(與角色= 'd' A用戶) position_id,這是描述的地方)

    class Dentist extends AppModel public $ hasOne ='Applicant'; }

    class申請人擴展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 

沒有更喜歡留外連接位置...

但是如果我在我的ApplicantsContr中使用$this->Applicant->find('all'); oller,我得到了左外連接位置...

如何設置模型的聯繫,以獲得來自我的DentistsController的表「位置」的聯接語句。

謝謝大家。

回答

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' => '' 
     ), 


    );