在Yii中无法在模型中保存空字符串

时间:2016-11-01 06:20:23

标签: php yii

我的代码非常简单:

if ($code) {
    $code->code = $_POST['code'];
    $code->save();
}

$_POST['code']具有"12345"abcde之类的值时,它会毫无问题地保存。但是当$_POST['code']""时,就没有保存。所以,我不能为这个字段设置空字符串来替换以前的值。我做错了什么?

4 个答案:

答案 0 :(得分:0)

检查您的验证规则。您可以定义一些验证规则,请检查它。

或打印模型验证错误

public Status StartProcess(string outputFolder)
{
    try
    {
        return service.StartProcess(outputFolder);
    }
    catch (System.ObjectDisposedException) // occurs, when client closed the connection previously
    {
        return Status.ClientDisconnected;
    }
    catch (System.ServiceModel.CommunicationObjectFaultedException) // occurs, when server unexpectedly closed the connection previously
    {
        return Status.CommunicationFailed;
    }
}

答案 1 :(得分:0)

在数据库中保存数据时,使用 try-catch 。这样做会显示错误/问题

if ($code) {
    $code->code = $_POST['code'];

    try {
        $code->save();
    } catch (Exception $e) {
        print_r($e);
    }
}

答案 2 :(得分:0)

确保验证规则中不需要模块的code属性。 例如:

public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('code', 'safe'),//or some other validation as below
        //array('code', 'length', 'max'=>255),

    );
}

答案 3 :(得分:0)

请更改您的代码,

if ($code) {
if($_POST['code'] != ""){
$code->code = $_POST['code'];
}else{
$code->code = "";
}
$code->save();
}