HABTM problem in Cakephp 2.x

Asked

Viewed 147 times

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 model Setor is worth 1?

  • 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?

  • 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

2 answers

1

Try adding on the line above the find this code snippet:

$this->Setor->bindModel( array('hasMany' => array('Veiculo') ) );

0

There on your controller I saw you use this:

$options = array('conditions' => array('Setor.' . $this->Setor->primaryKey => $id));

Just call it that:

$options = array('conditions' => array('Setor.veiculo_id' => $id));

Or whatever the field, but you’re calling the array index as the condition argument in find.

Prefer to use array('Model.field' => 'Value');

  • Hello, thank you for the answer. It still didn’t work. I would like to get the related vehicles at the time I am viewing the data from the sectors. The strange thing is that when I do the same thing for the Vehicle, the sectors are returned. The code is following the same logic. This is really a problem for me. att.

Browser other questions tagged

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