I’m unable to update a field in Cakephp with $this->Model->saveField()

Asked

Viewed 121 times

1

I’m unable to update a certain field in Cakephp.

When the $this->OrdemServico->saveField(), he simply does not perform.

Code:

class OrdemServico extends AppModel {

    public $name = "OrdemServico";
    public $useTable = "ordens_servico";
    public $belongsTo = array(
        "Cliente" => array(
            "className" => "Cliente",
            "foreignKey" => "id_cliente"
        ),
        "Responsavel" => array(
            "className" => "Usuario",
            "foreignKey" => "responsavel"
        ),
        "Equipamento" => array(
           "className" => "Equipamento",
           "foreignKey" => "equipamento"
        ),
        "ModoEntrega" => array(
           "className" => "ModoEntrega",
           "foreignKey" => "modo_entrega"
        )
    );

}


class OrdemServicoController extends AppController {
     public function cancelar() {
        try {
            $this->layout = "ajax";
            $this->autoLayout = false;

            $data = $this->request->data;
            $id = $data["question"]["parameter"];
            $destino = unserialize($data["question"]["callback"]);

            $this->OrdemServico->id = $id;
            $this->OrdemServico->saveField("OrdemServico.cancelado", true);

            $this->Dialog->alert("A ordem de serviço foi cancelada com sucesso.");
            $this->redirect($destino);
        } catch (Exception $ex) {
            $mensagem = "Ocorreu um erro no sistema ao atualizar a ordem de serviço.";

            $this->Dialog->error($mensagem, $ex->getMessage());
            $this->redirect(array("action" => "index"));
       }
    }
}

Question.ctp

<div id="dialog-question" class="modal-dialog">
    <?php
    echo $this->Form->create(null, array(
        "url" => array(
            "controller" => "ordem_servico",
            "action" => "cancelar"),
        "id" => $form_name,
        "role" => "form"
    ));

    echo $this->Form->hidden("question.parameter");
    echo $this->Form->hidden("question.callback", array("value" => serialize($retorno)));
    ?>

    <div class="modal-content">
        <div class="modal-header" style="cursor: move">
            <button id="btn-question-close" type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title"><i class="fa fa-bell"></i>&nbsp;&nbsp;Sistema de Ordem de Serviço</h4>
        </div>
        <div class="modal-body">
            <p><?= h($message) ?></p>
        </div>
        <div class="modal-footer">
            <button id="btn-cancel-modal" type="button" class="btn btn-danger"><?= $buttons["cancel"] ?></button>
            <button id="btn-default-modal" type="submit" class="btn btn-success"><?= $buttons["ok"] ?></button>
        </div>
    </div>
    <?= $this->Form->end() ?>

I would like to know what is happening, because the system can not update the field at all.

  • Switch to catch (Pdoexception $ex), otherwise you will never know if the error is in the query

  • I already figured out the origin of the problem. It was the ID that wasn’t being searched for from CTP. Thanks for the help.

  • Thanks. I was able to solve the problem. It was the ID that was not being caught when calling the method. But I will remember to put to Pdoexception next time. And thanks for the tip.

1 answer

0

You’re passing the ID(int) of the field line you want to update ?, $data["question"]["parameter"]; matches the model’s primary key ?

Example

function some($id = null){
       //...
       $this->Model->id = $id;
       $this->Model->saveField("Model.campo", true);
       //....
}

action.ctp

echo $this->Form->create();
echo $this->Form->input('Model.id', array('type' => 'hidden'));
echo $this->Form->end('Enviar');

Browser other questions tagged

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