How to save a HABTM after the save in the controller?

0

I have a table on Users HABTM Solicitations . After salver my request, I want to include a new data in table solicitations_users

if($this->request->is('post')) {

$this->Solicitation->save($this->request->data);

// Aqui é meu codigo onde pego a ID do usuário, 6 por exemplo
// Agora eu quero salvar a User ID na "Solicitation" que acabei de criar.

// O codigo abaixo nao funciona

 $data = array($this->data['User']['id'] => 6);
 $this->Solicitation->save($data);

How can I save a new user_id on the associations_users table after $this->save() associated with solicitation that I just created ?? Vlw

    
asked by anonymous 17.03.2014 / 14:28

1 answer

1

To save associations_users just use

if($solicitationId = $this->Solicitation->save($this->request->data)){
    $data = array(
        "user_id" => $this->data['User']['id'],
        "solicitation_id" => $solicitationId
    );
    $this->AssociationsUser->save($data);
}

I imagine it's what you need.

Every method save returns the id of the last record that was saved, so you can take this data and use it in another save or update

    
17.03.2014 / 15:39