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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀