Problem with HABTM in Cakephp 2.x

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 the 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 being listed in Vehicle, but I know there is related data. When I search the Vehicle, it returns me to the related Sectors. But not the other way around.

My Industry 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 Vehicle Model:

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 with Contain, relationships with vehicles are not returned.

Any suggestions?

    
asked by anonymous 26.02.2014 / 22:28

2 answers

1

Try adding in the line above the find of this code snippet:

$this->Setor->bindModel( array('hasMany' => array('Veiculo') ) );
    
31.03.2014 / 23:45
0

There in your controller I saw you use this:

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

Call it just like this:

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

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

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

    
28.02.2014 / 12:45