duanjiaoxi4928 2014-05-08 12:42
浏览 28
已采纳

不能从Yii中将SQL中的位值设置为NULL [重复]

This question already has an answer here:

Whenever I try to do this....

if($filter[11]['card'] == 'BOTH'){
    $marketingFilter->card_mandatory = null;
} else {
    $marketingFilter->card_mandatory = $filter[11]['card'] == 'YES' ? true : false;
}

...while $filter[11]['card'] = 'BOTH', the value in the SQL table, after it runs the save method, does not change to NULL.

card_mandatory is a BIT value.

Once the attribute $marketingFilter->card_mandatory is set to null, it never changes.

The code I wrote on top works perfectly, it's just when I save that the value never gets entered in correctly. Am I doing something wrong?

------ADDED MORE CODE

Yii::app()->db->beginTransaction();
$marketingFilter = new MarketingFilter();
$marketingFilter->name                          = $filter[22]['name'];
date_default_timezone_set('Canada/Montreal');
$date = date('m-d-Y H:i:s', time());
$marketingFilter->date_created                  = $date;
$marketingFilter->created_by                    = $loggedAccount->last_name . ", " .                    $loggedAccount->first_name;
$marketingFilter->total_members                 = '0';
if(count($filter[6]['gender']) == 2) {
     $marketingFilter->gender                    = $filter[6]['gender'][1];
}
if(count($filter[7]['language']) == 2) {
     $marketingFilter->language                  = $filter[7]['language'][1];
}
$marketingFilter->save();
Yii::app()->db->currentTransaction->commit();

card_mandatory is not a required field and it allows nulls. In SQL Manager I can enter in null manually and it works fine. card_mandatory is the only BIT value in $marketingFilter. I add nulls the same way in my code to other values (ex. nvarchar, datetime2 etc..), but this one is giving me a brain aneurysm.

</div>
  • 写回答

1条回答 默认 最新

  • dpwgzi7987 2014-05-09 13:03
    关注

    Use new CDbExpression('NULL') (docs) instead of NULL:

    $marketingFilter->card_mandatory = new CDbExpression('NULL');
    

    You need same expression when you create database queries.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog