How to change the field of a table by referencing values from another table. Using cakephp 1.3

Asked

Viewed 111 times

1

I have an association of tables projeto > auto > anexo. In the project table I have a field cod_status, and in the attached table I have a field status. How do I get when I edit the field Anexo.status to 'I', the Projeto.cod_status be changed to 5 automatically.

Controller edit

   function edit($id = null)
{
    if (!$id && empty($this->data)) {
        $this->Session->setFlash(sprintf(__('%s inválido.', true), 'Anexo'));
        $this->redirect(array('action' => 'index'));
    }
    if (!empty($this->data)) {
        if ($this->Anexo->save($this->data)) {
            $this->Session->setFlash(sprintf(__('%s alterado com sucesso.', true), 'Anexo'), 'default', array('class' => 'success'));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(sprintf(__('O %s não pode ser salvo. Por favor, tente novamente.', true), 'anexo'));
        }
    }
    if (empty($this->data)) {
        $this->data = $this->Anexo->read(null, $id);
    }
}

1 answer

0

Just checking if in the data to save your field is with the desired value and make a condition so that if you are, save the Project too, I believe it works:

inside your if ($this->Anexo->save($this->data)) { insert the following code according to the operation I mentioned above.

if ($this->data['status'] === 'l'){
  $this->Projeto->set(array('cod_status' => 5));
  $this->Projeto->save();
}

Perhaps the data format on $this->Data is different than just an array with the fields of the model in question, and if so, please debug and change it to fit this format, it may be something like $this->data['Anexo']['status'] also.

Browser other questions tagged

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