0

I'm having problems with HABTM + cakephp.

In my app i have this models (with relation definition):

Cliente:

public $hasAndBelongsToMany  = array(
    'TiposComercio' => array(
        'className'             => 'TiposComercio',
        'joinTable'             => 'tipos_comercio_cliente',
        'foreignKey'            => 'cliente',
        'associationForeignKey' => 'tipo_comercio'
    )
);

TiposComercio:

public $hasAndBelongsToMany = array(

    'Cliente' => array(
        'className' => 'Cliente',
        'joinTable' => 'tipos_comercio_cliente',
        'foreignKey' => 'tipo_comercio',
        'associationForeignKey' => 'cliente'
    )
);

When the form is posted, i have in array $this->request->data this:

array(

    'Cliente' => array(
        'password' => '*****',
        'razao_social' => 'Teste',
        'responsavel' => 'responsavel',
        'cidade' => '1',
        'cep' => '13560201',
        'logradouro' => 'Log test',
        'numero' => '',
        'bairro' => '',
        'complemento' => '',
        'atividades' => '',
        'username' => 'username11',
        'TiposComercio' => array(
            (int) 0 => '1',
            (int) 1 => '4'
        )
    )

)

When i execute $this->Cliente->saveAll($this->request->data); in ClientesController I have the following problem:

  • Just Cliente data is saved. Nothing of TiposComercio is saved.

What's wrong?

Thanks!

matheusvmbruno
  • 2,140
  • 3
  • 15
  • 20

1 Answers1

2

The structure of your data array seems to be wrong. Have a look at the manual for saving HABTM data: http://book.cakephp.org/2.0/en/models/saving-your-data.html#saving-related-model-data-habtm

Based on the example, your data array should be formed like this:

array(
    'Cliente' => array(
        'password' => '*****',
        'razao_social' => 'Teste',
        'responsavel' => 'responsavel',
        'cidade' => '1',
        'cep' => '13560201',
        'logradouro' => 'Log test',
        'numero' => '',
        'bairro' => '',
        'complemento' => '',
        'atividades' => '',
        'username' => 'username11',
    ),
    'TiposComecio' => array(
        (int) 0 => '1',
        (int) 1 => '4'
    )
)
Armon
  • 358
  • 2
  • 9