How to save a HABTM after save to controller?

Asked

Viewed 343 times

0

I have a table on Users HABTM Solicitations. After saving my request, I want to include a new data in the 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 to save a new user_id on the table associations_users after the $this->save() associated with solicitationthat I just created? Vlw

  • first question is "the uqe you want to save?"

  • edited my question. vlw

  • 1

    could you explain the context by which you have an update to the record you just created? Wouldn’t it be simpler to pass the Id in the registry that was included? What happens when you run your code? gives some error or does not save any record?

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 that’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 in another save or update

Browser other questions tagged

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