duandao1931 2013-06-24 02:22
浏览 25
已采纳

Cakephp - 无法将布尔值更新为true? [关闭]

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

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

it fails model validation: Model->Task

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

TaskController.php

This works:

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.'));
        }

This does not:

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());  
   }  
} 

Here is the call from 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: field 'active' is type "tinyint".

Also, the checkbox form control generated by Bake in Views/Tasks/edit.ctp works just fine.

I have also tried the following:

 ($this->Task->save('active', 1)
 ($this->Task->save('active', true)
 ($this->Task->save('active', 'true')
 ($this->Task->save('active', '2')

This:

 ($this->Task->save('active', `1`) //notice the tic marks

seems to bypass validation, but does not update the database.

  • 写回答

2条回答 默认 最新

  • doutuobao4004 2013-06-24 10:01
    关注

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

    Well, I doubt it :)

    It might not give an error but it's not going to do what you expect. That's going to skip validating and try to process 'active' as an array. The consequence of doing that is it's probably only bumping the modified field time.

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

    That's dependent on your validation rules, but regardless that syntax is not going to do what you're expecting.

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

    Please refer to the documentation, your parameters are wrong. You're passing the string "active" as your data, and your boolean-ish value as the value of $validate.

    This would work (or, not fail as being syntactically wrong):

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

    or this:

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

    or by using saveField:

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

    If in doubt - use bake, or compare to the code it generates.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用