3
I’m having problems while searching my view method, where I have the following search:
public function view($id = null) {
if (!$this->Setor->exists($id)) {
throw new NotFoundException(__('Parâmetro inválido!'));
}
$options = array('conditions' => array('Setor.' . $this->Setor->primaryKey => $id));
$this->set('setor', $this->Setor->find('first', $options));
}
I have the result in debug:
array(
(int) 0 => array(
'Setor' => array(
'id' => (int) 1,
'nome' => 'Diretoria de Modernização Administrativa',
'secretaria_id' => (int) 2
),
'Secretaria' => array(
'id' => (int) 2,
'codigo' => '00004210',
'nome' => 'Secretaria de Estado de Planejamento'
),
'Veiculo' => array()
)
The problem is that nothing is listed in Vehicle, but I know that there is related data. When I do the Vehicle search, it returns me the related Sectors. But not the other way around.
My Sector model is defined as follows:
public $belongsTo = array(
'Secretaria' => array(
'className' => 'Secretaria',
'foreignKey' => 'secretaria_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Status' => array(
'className' => 'Status',
'foreignKey' => 'status_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
public $hasAndBelongsToMany = array(
'Veiculo' => array(
'className' => 'Veiculo',
'joinTable' => 'setores_veiculos',
'foreignKey' => 'setor_id',
'associationForeignKey' => 'veiculo_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
)
);
The model of Vehicle:
public $belongsTo = array(
'TipoVeiculo' => array(
'className' => 'TipoVeiculo',
'foreignKey' => 'tipo_veiculo_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Status' => array(
'className' => 'Status',
'foreignKey' => 'status_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
public $hasMany = array(
'Device' => array(
'className' => 'Device',
'foreignKey' => 'veiculo_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
public $hasAndBelongsToMany = array(
'Setor' => array(
'className' => 'Setor',
'joinTable' => 'setores_veiculos',
'foreignKey' => 'veiculo_id',
'associationForeignKey' => 'setor_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
),
'Condutor' => array(
'className' => 'Condutor',
'joinTable' => 'condutores_veiculos',
'foreignKey' => 'veiculo_id',
'associationForeignKey' => 'condutor_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
)
);
Even using Contain, relations with Vehicles are not returned.
Any suggestions?
You made sure that the property
recursive
of the modelSetor
is worth 1?– Paulo Rodrigues
Yes, I already changed the value to 1, but still the vehicles are not listed. Could it be a convention problem? Or in the translation of the model into Portuguese?
– Ricardo Farias
For this, your file
bootstrap.php
needs to be configured because of the change from singular to plural names like sector, driver and etc. See here: http://book.cakephp.org/2.0/en/development/configuration.html#inflection-Configuration– Paulo Rodrigues