我尝试创建一个编辑视图,允许用户编辑名为ABC的模型的条目。一旦用户打开某一行的编辑掩码,它就应该锁定该行。我在数据库中有一个名为'locked'的tinyint(1)值,它将执行此操作。这是代码的一部分。它发生在saveField方法上,我已经检查过了。这很奇怪,因为在正确的记录中改变了价值!但不知怎的,它试图两次做同样的任务,我不知道为什么。
function edit($id = null) {
// select the
$this->ABC->id = $id;
$session = $this->Session->read();
$this->set('locked',false);
// save or read the data
if (empty($this->data)) {
$this->data = $this->ABC->read();
// locking
if ($this->data["ABC"]["locked"] == true) {
$this->set('usercanedit', false);
$this->set('locked', true);
} else {
$this->ABC->saveField('locked', true);
}
}
}
当我用这个替换saveField代码时(这当然是愚蠢的,仅用于测试)它可以工作。这以某种方式证明了编辑方法被调用了两次。一旦没有参数,或者有一个错误的参数......
if($this->ABC->id == 13)
$this->ABC->saveField('locked', true);
有人有想法吗?
答案 0 :(得分:1)
尝试在saveField调用之前设置id,就像书中所说:
在调用saveField()之前设置模型的ID($ this-> ModelName-> id = $ id)。
在你的情况下:
} else {
$this->ABC->id = $id; // ID should be set right before the saveField call
$this->ABC->saveField('locked', true);
}