Upgrade with HABTM model - Cakephp

Asked

Viewed 164 times

0

I am having trouble entering or changing data using the HABTM model, it does not recognize 3 fields, but is recognizing hidden fields normally.

Follow the files with programming, are 3 tables, one with the data of the clients, others with the data of the options chosen (usuario, senha, id_opcao, etc) and another with which option it is (Control Panel, database, etc)

php client - model

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

Clientescontroller.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 (abridged)

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 customer data is changed normally, but on opcoes_cliente only inserts (if not) the opco_id and cliente_id. The fields host, user and password are empty, even if some value is filled.

  • Does it enter the empty die or does it not enter??? And has tried to change only to $this->Form->end();

  • It inserts with the opco_id and cliente_id with the right id’s, but the host, user and password are empty, if I delete these records from the database and save the form, it inserts again. On the form end is that I cut a piece to summarize, but there is the class of the button and the Label nothing that interferes with the functioning

  • You are loading the related model into your controler??

  • Yes, normally, so much so that to pull the data works smoothly, I find it strange because the insertion occurs normally, but only for two fields, the other three does not receive the value and is like this, wrong input name shouldn’t be... would it have to pass some other parameter over? Another detail that I did not add, in the appmodel I overloaded the saveAssociated method according to http://bakery.cakephp.org/articles/ccadere/2013/04/19/save_habtm_data_in_a_single_simple_format so he identified the foreign key

  • Try using the default cake layout, and put that query in your question

  • the default layout, the view? well, I don’t know if I could interfere, because I don’t see anything that runs too far from a standard look, the most I use is bootstrap with menu and such... Anyway, I don’t know if you have any different method to save when using the HABTM model

  • Yeah, I don’t understand why you don’t save the other data, I used debugkit and the values are sent normally along with the cliente_id and option

Show 2 more comments

1 answer

1

I managed to solve the problem

In my model (.php client) I left with 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 that the traditional model works like this.

My.ctp edit 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 variable $i which is an auto increment so that it can save directly

In Clientecontroller.php, in the part that saves

$this->Cliente->saveAssociated($this->request->data)

Browser other questions tagged

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