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.

有帮助吗?

解决方案

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

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

You need same expression when you create database queries.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top