Problem with HABTM in Cakephp 2.x

Question:

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, nothing is being listed under Vehicle, but I know the related data exists. When I perform the Vehicle search, it returns 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' => '',
    )
);

A 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 using Contain, relations with Vehicles are not returned.

Any suggestion?

Answer:

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

$this->Setor->bindModel( array('hasMany' => array('Veiculo') ) );
Scroll to Top
AllEscort