CakePHP 2请帮助模特协会

时间:2013-04-23 06:06:50

标签: cakephp cakephp-2.1

假设我有3个型号。

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

    class Dentist扩展AppModel {   public $ hasOne ='申请人'; }

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

    类位置扩展AppModel { }

  4. 当我在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”。

    谢谢大家。

1 个答案:

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


    );