Update with HABTM template - CakePHP

0

I'm having trouble inserting or changing data using the HABTM template, it does not recognize 3 fields, but is recognizing hidden fields normally.

It follows the files with programming, they are 3 tables, one with the data of the clients, others with the data of the chosen options ( usuario , senha , id_opcao , etc) and another with which option is (Control Panel, Database, etc.)

client.php - model

public  $hasAndBelongsToMany  =  array (
    'opcoes_clientes'  =>  array (
        'className'  =>  'opcoes' ,
    )
);

ClientsController.php

if ($this->request->is('post') || $this->request->is('put')) {
        if ($this->Cliente->save($this->request->data)) {
            $this->Cliente->saveAssociated($this->request->data);
            $this->Session->setFlash('O cliente foi editado com sucesso!', 
                'alert', array( 'plugin' => 'BoostCake', 'class' => 'alert-success' ));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash('O cliente não foi editado. Tente novamente.', 'alert', array(
                'plugin' => 'BoostCake', 'class' => 'alert-error'
            ));
        }
    } else {
        $this->request->data = $this->Cliente->read();
        $this->set('clientes', $this->Cliente->read());
    }

edit.ctp (summarized)

echo $this->Form->create("Cliente");
echo $this->Form->input('nome', $options);
echo $this->Form->input('email', $options);
echo $this->Form->input('telefone', $options);
echo $this->Form->input('mais', $options);
foreach($opcoes as $data):
    echo $this->Form->hidden('opcoes_clientes.opco_id', array('value'=> $id));
    echo $this->Form->hidden('opcoes_clientes.cliente_id', array('value'=> $clientes['Cliente']['id']));
    echo $this->Form->input('opcoes_clientes.host', $options);
    echo $this->Form->input('opcoes_clientes.user', $options);
    echo $this->Form->input('opcoes_clientes.password', $options);
endforeach;
echo $this->Form->end($options);

All client data is changed normally, but in opcoes_cliente it only inserts (if it does not) the opco_id and cliente_id . The fields host , user and password are empty even if some value is filled in.

    
asked by anonymous 29.04.2014 / 16:38

1 answer

1

I was able to solve the problem

In my model (client.php) I left with the unique = true

public  $hasAndBelongsToMany  =  array (
    'opcoes_clientes'  =>  array (
        'className'  =>  'opcoes',
        'unique' => true,
        'foreignKey'             => 'cliente_id',
        'associationForeignKey'  => 'opco_id'

    )
);

The only thing he's deleting and inserting again, it seems like the traditional template works like this.

My edit.ctp I passed the two keys

echo $this->Form->hidden('opcoes_clientes.'.$i.'.opco_id', array('value' => $id));
echo $this->Form->hidden('opcoes_clientes.'.$i.'.cliente_id', array('value' => $clientes['Cliente']['id']));

And all fields have the $ i variable which is an auto increment so you can save correctly

In the ClientController.php, in the part that saves

$this->Cliente->saveAssociated($this->request->data)
    
06.05.2014 / 21:32