我无法更新CakePHP中的特定字段。下面的代码如下。当您运行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">×</span></button>
<h4 class="modal-title"><i class="fa fa-bell"></i> 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() ?>
}
{{1}}
我想知道发生了什么,因为系统无法以这种方式更新任何字段。
答案 0 :(得分:1)
$this->OrdemServico->saveField("OrdemServico.cancelado", true);
saveField不期望以模型为前缀的字段名称。上面的语法等同于调用:
$this->OrdemServico->save([
'OrdemServico' => [
'id' => x,
'OrdemServico.cancelado' => true
]
]);
那不会奏效。
要使用saveField,只需传递一个字段名称:
$this->OrdemServico->saveField("cancelado", true);
或者,请致电保存。