saveField 不更新记录
saveField does not update the record
我无法更新 CakePHP 中的特定字段。下面的代码。当您 运行 $this->OrdemServico->saveField()
时,它根本不起作用。
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() ?>
我不知道这是怎么回事,因为系统无法以这种方式更新任何字段。
saveField 需要一个字段名
$this->OrdemServico->saveField("OrdemServico.cancelado", true);
saveField 不需要以模型为前缀的字段名称。上面的语法相当于调用:
$this->OrdemServico->save([
'OrdemServico' => [
'id' => x,
'OrdemServico.cancelado' => true
]
]);
那不行。
要使用 saveField,只需将字段名称传递给它:
$this->OrdemServico->saveField("cancelado", true);
或者,调用保存。
我无法更新 CakePHP 中的特定字段。下面的代码。当您 运行 $this->OrdemServico->saveField()
时,它根本不起作用。
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() ?>
我不知道这是怎么回事,因为系统无法以这种方式更新任何字段。
saveField 需要一个字段名
$this->OrdemServico->saveField("OrdemServico.cancelado", true);
saveField 不需要以模型为前缀的字段名称。上面的语法相当于调用:
$this->OrdemServico->save([
'OrdemServico' => [
'id' => x,
'OrdemServico.cancelado' => true
]
]);
那不行。
要使用 saveField,只需将字段名称传递给它:
$this->OrdemServico->saveField("cancelado", true);
或者,调用保存。