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'));
}