使用CodeIgniter将NULL插入数据库的正确方法

Chr*_*ris 10 php sql-server codeigniter extjs

我有ExtJS从combofield发布数据到CodeIgniter.CodeIgniter读取发布的数据并运行db-> insert以更新mssql数据库.

问题是,如果用户没有从组合框中选择一个选项,它将被发送到CodeIgniter,则Codeignighter会将这条发布的数据作为空字符串读取并尝试将其写入数据库中的int字段,从而导致错误.

我相信我需要CodeIgniter将字符串视为NULL(如果它为空)和数字(如果是空的话).我已经尝试使用php类型juggling(int)来转换它但它使它为0.这是一个无效的值,因为它与我的数据库中我的一对多int字段中的任何选择都不匹配.

有没有办法让CodeIgniter将空字符串视为NULLS而不是空字符串?

[编辑添加代码]

{
//ExtJS combo object
xtype: 'combo',
id: 'Referrer',
store: new Ext.data.Store({
    proxy: new Ext.data.HttpProxy({
        url: '/referrals/index.php/referrers/read',
        method: 'POST'
    }),
    reader: new Ext.data.JsonReader({
        root: 'results',
        fields: [
            {name: 'ID'},
            {name: 'Referrer'}
        ]
    })
}),
displayField: 'Referrer',
valueField: 'ID',
value: 1,
typeAhead: true,
hiddenName: 'Referrers_ID',
mode: 'remote',
triggerAction: 'all',
fieldLabel: 'Referrer',
selectOnFocus: true,
anchor: '100%'
}

//CI CODE TO GRAB FROM POST INTO AN ARRAY THEN OUT TO THE DB
public function create(){
    $data = array(  
        'FirstName' => $this->input->post('FirstName', false),
        'LastName' => $this->input->post('LastName', false),
        'DOB' => $this->input->post('DOB', false),
        'Email' => $this->input->post('Email', false),
        'Phone' => $this->input->post('Phone', false),
        'StudentNo' => $this->input->post('StudentNo', false),
        'Sex' => $this->input->post('Sex', false),
        'Advisors_ID' => $this->input->post('Advisors_ID', false),
        'DateSeen' => $this->input->post('DateSeen', false),
        'Classifications_ID' => join(",", $this->input->post('Classifications_ID', false)),
        'Referrers_ID' => $this->input->post('Referrers_ID', false),
        'Referrals' => $this->input->post('Referrals', false),
        'ReferralNotes' => $this->input->post('ReferralNotes', false),
        'Registration1' => $this->input->post('Registration1', false),
        'Registration2' => $this->input->post('Registration2', false),
        'Notes' => $this->input->post('Notes', false)
    );

    $this->db->insert('Clients', $data);
    $insert_id = $this->db->insert_id();
}
Run Code Online (Sandbox Code Playgroud)

Joh*_*hnP 11

假设您的变量名为$ myvar 并假设您在此之前已完成所有错误检查,验证和转换.

$myvar  = empty($myvar) ? NULL : $myvar;
Run Code Online (Sandbox Code Playgroud)

这将为codeignitor提供正确的数据.