0

假设我有 3 个模型。

  1. 牙医(角色='D' 的用户)
  2. 申请人(有关每个用户的更多详细信息,例如地址、电话、兴趣位置等)
  3. 职位(在申请表中我只存储 position_id,这是描述的地方)

    牙医类扩展 AppModel { public $hasOne = 'Applicant'; }

    类申请人扩展 AppModel { public $belongsTo = array('Dentists', 'Position'); }

    类位置扩展 AppModel { }

当我$this->Dentist->find('all');在我的 DentistsController 中使用时,我在牙医的视图中遇到了问题,因为 SQL 就像

select *
from dentists left outer join applicants
  on dentists.id = applicants.dentist_id

不再像左外连接位置...

但是如果我$this->Applicant->find('all');在我的申请人控制器中使用,我得到了左外连接位置......

如何设置模型关联以从我的 DentistsController 获取连接语句到表“位置”。

谢谢大家。

4

1 回答 1

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


    );
于 2013-04-23T06:39:57.350 回答