无法将模型属性保存为null?

时间:2017-05-10 10:47:11

标签: yii2

这是我的模型规则

public function rules() {
        return [
            [['header_image', 'profil_picture'], 'default', 'value' => null],
            [['yahoo_id', 'whats_app_id', 'bbm_id'], 'string', 'max' => 20],
            [['bbm_id'], 'match', 'pattern' => '/^[a-zA-Z0-9]+$/', 'message' => 'Alpha numeric only'],
            [['header_image_file', 'profil_picture_file'], 'file', 'skipOnEmpty' => true, 'extensions' => 'png, jpeg, jpg, bmp', 'maxSize' => 10240 * 20240 * 2],
            [['deskripsi_toko'], 'string', 'max' => 300],
            [['agent_id', 'nama_toko', 'tag_line', 'header_image', 'profil_picture', 'yahoo_id', 'whats_app_id', 'bbm_id', 'deskripsi_toko'], 'filter', 'filter' => function($value) {
                    return BeoHelper::replace_mc($value);
                }],
        ];
    }

这是我的控制器

    $storeSetting->header_image = null;
    if($storeSetting->save()){
      return $this->redirect(Yii::$app->request->referrer);
    }

replace_mc函数

public static function replace_mc($str)
    {
        $new_data = preg_replace('/[^A-Za-z0-9\-\ \.\:\@\+]/', '', $str);
        return $new_data;
    }

记录保存成功,但header_image是空字符串而不是空?

它应该将header_image设置为null,我在哪里做错了?

提前致谢。

1 个答案:

答案 0 :(得分:0)

尝试replace_mc()中的其他条件:

public static function replace_mc($str)
{
    return $str === null 
        ? $str 
        : preg_replace('/[^A-Za-z0-9\-\ \.\:\@\+]/', '', $str);
}
相关问题