A Model with 2 relationships but only 1 appears in the view

Asked

Viewed 39 times

0

I have a model called doctor that belongs , to a city , and belongs to a specialty code :

App::uses('AppModel', 'Model');
class Medico extends AppModel { 
public $belongsTo = array(
    'Cidade' => array(
        'className' => 'Cidade',
        'foreignKey' => 'cidade_id'
    ),
    'Especialidade' => array(
        'className' => 'Especialidade',
        'foreignKey' => 'especialidades_id'
    )
);
}

Beauty so far all right the city model has many doctors:

App::uses('AppModel','Model');
 class Cidade extends AppModel
 {
   public $belongsTo = array(
    'Estado' => array(
        'className' => 'Estado',
        'foreingKey' => 'estado_id'
    ));

public $hasMany= array(
    'Medicos'=>array(
     'className'=>'Medico',
     'foreingKey'=>'cidade_id'
    )
);
}

The specialty model has many doctors

class Especialidade extends AppModel {

public $useTable = 'especialidades';
public $hasMany = array(
    'Medicos' => array(
        'className' => 'Medico',
        'foreignKey' => 'especialidades_id'
    )
);
}

But when trying to register a doctor when adding the specialty does not appear, the city appears normal but the specialty does not, what can be wrong? below controller code

class MedicosController extends AppController {
public $components = array('Paginator', 'Flash');
public $helpers = array('Html','Form','Flash');
  public function add() {
    if ($this->request->is('post')) {
        $this->Medico->create();
        if ($this->Medico->save($this->request->data)) {
            $this->Flash->success(__('The medico has been saved.'), 'default', array('class' => 'alert alert-success'));
            return $this->redirect(array('action' => 'index'));
        } else {
            $this->Flash->error(__('The medico could not be saved. Please, try again.'), 'default', array('class' => 'alert alert-error'));
        }
    }
    $cidades = $this->Medico->Cidade->find('list');
    $this->set(compact('cidades'));
    $espcialidades = $this->Medico->Especialidade->find('list');
    $this->set(compact('espcialidades'));


}

1 answer

0


After a while testing I understood why I solved so,in medical controller I put the model name in plural when calling find method()

$especialidades = $this->Medico->Especialidades->find('list',array('fields'=>array('id','especialidade')));
    $this->set(compact('cidades', 'especialidades'));

And that’s because the model relationship always has an alias and this alias in the model is plural follows the code below:

public $belongsTo = array(
    'Cidade' => array(
        'className' => 'Cidade',
        'foreignKey' => 'cidade_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'Especialidades' => array(
        'className' => 'Especialidade',
        'foreignKey' => 'especialidades_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

Good finally solved the problem!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.