这有效: ($this->Task->save('active', '0')
这不会: ($this->Task->save('active', '1')
模型验证失败:模型->任务
'active' => array(
'boolean' => array(
'rule' => array('boolean'),
),
),
任务控制器.php
这有效:
public function deactivate ($id = null) {
$this->Task->id = $id;
if (!$this->Task->exists()) {
throw new NotFoundException(__('Invalid task'));
}
$this->request->onlyAllow('post');
if ($this->Task->save('active', '0')) {
$this->Session->setFlash(__('The task has been saved'));
$this->redirect($this->referer());
} else {
$this->Session->setFlash(__('The task could not be saved. Please, try again.'));
}
这不会:
public function activate ($id = null) {
$this->Task->id = $id;
if (!$this->Task->exists()) {
throw new NotFoundException(__('Invalid task'));
}
$this->request->onlyAllow('post');
if ($this->Task->save('active', 1)) {
$this->Session->setFlash(__('The task has been saved'));
$this->redirect($this->referer());
} else {
$this->Session->setFlash(__('The task could not be saved. Please, try again.'));
$this->redirect($this->referer());
}
}
这是来自 View/Tasks/index.ctp 的调用:
<?php
if ($task['Task']['active'] == 1){
echo $this->Form->postLink(__('Deactivate'), array('action' => 'deactivate', $task['Task']['id']),null, __('Are you sure you want to return # %s to the project?', $task['Task']['id']));
} else {
echo $this->Form->postLink(__('Activate'), array('action' => 'activate', $task['Task']['id']),null, __('Are you sure you want to send # %s to your todo list?', $task['Task']['id']));
}
?>
mysql db:字段“活动”是类型“tinyint”。
此外,由 Bake 在 Views/Tasks/edit.ctp 中生成的复选框表单控件也可以正常工作。
我还尝试了以下方法:
($this->Task->save('active', 1)
($this->Task->save('active', true)
($this->Task->save('active', 'true')
($this->Task->save('active', '2')
这个:
($this->Task->save('active', `1`) //notice the tic marks
似乎绕过验证,但不更新数据库。