0
votes

Let's say I have 3 models.

  1. Dentist (A user with with role='D')
  2. Applicant (More details on each user such as address, phone, interested_position, etc)
  3. Position (In table applicants I only store position_id, this is where the description)

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

    class Applicant extends AppModel { public $belongsTo = array('Dentists', 'Position'); }

    class Position extends AppModel { }

I have a problem in Dentists's view when I use $this->Dentist->find('all'); in my DentistsController because the SQL just like

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

No more like left outer join positions...

But if I use $this->Applicant->find('all'); in my ApplicantsController, I got left outer join positions...

How to set the model assosiation to get join statement to table "positions" from my DentistsController.

Thanks all.

1

1 Answers

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


    );