gpt4 book ai didi

php - Cakephp - 无法将 bool 值更新为 true?

转载 作者:行者123 更新时间:2023-11-29 13:46:57 26 4
gpt4 key购买 nike

这有效:($this->任务->保存('active', '0')

这不会:($this->任务->保存('active', '1')

模型验证失败:模型->任务

'active' => array(
'boolean' => array(
'rule' => array('boolean'),
),
),

TaskController.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:字段“active”的类型为“tinyint”。

此外,在 Views/Tasks/edit.ctp 中 Bake 生成的复选框表单控件也可以正常工作。

我还尝试了以下方法:

 ($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

似乎绕过验证,但不更新数据库。

最佳答案

This works: ($this->Task->save('active', '0')

好吧,我对此表示怀疑:)

它可能不会给出错误,但它不会执行您期望的操作。这将跳过验证并尝试将 'active' 作为数组进行处理。这样做的结果是它可能增加修改字段时间。

This does not: ($this->Task->save('active', '1')

这取决于您的验证规则,但无论如何该语法都不会达到您的预期。

Model::save(array $data = null, boolean $validate = true, array $fieldList = array())]

请引用the documentation ,你的参数错误。您将字符串“active”作为数据传递,并将 bool 值作为 $validate 的值传递。

这会起作用(或者,不会因为语法错误而失败):

$this->Task->save(array('active' => $value));

或者这个:

$this->Task->save(array('Task' => array('active' => $value)));

或使用 saveField:

$this->Task->saveField('active', $value)

如有疑问 - use bake ,或与它生成的代码进行比较。

关于php - Cakephp - 无法将 bool 值更新为 true?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17267209/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com