A Model with 2 relationships but in the view only 1

0

I have a model called doctor who 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 up there all right the model city 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'));


}
    
asked by anonymous 11.03.2017 / 23:07

1 answer

0

After a while testing I understood why I solved so, in the controller physicians I put the name of the model in the plural at the time of calling the find () method

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

And this is because the model the relationship always has an alias and this alias in the model was in the plural follows 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!

    
12.03.2017 / 02:14