response->format=Response::FORMAT_JSON;"; 2. Check whether the data passes the model's validation rules and print it before saving. Verification results."/> response->format=Response::FORMAT_JSON;"; 2. Check whether the data passes the model's validation rules and print it before saving. Verification results.">

Home  >  Article  >  PHP Framework  >  What should I do if yii save cannot save data?

What should I do if yii save cannot save data?

藏色散人
藏色散人Original
2020-07-23 11:21:513746browse

Yii save cannot save data solution: 1. Remove the statement "Yii::$app->response->format=Response::FORMAT_JSON;"; 2. Check whether the data passes the model verification rules, and print the verification results before saving.

What should I do if yii save cannot save data?

What should I do if yii save cannot save data?

Recommendation: "yii Tutorial"

## Specific questions:

Yii2 save failed to save [2.0 version]

public function actionIndex()
{
    $session = Yii::$app->session;
    Yii::$app->response->format=Response::FORMAT_JSON;
    $model = new PostSendInfo();
//        $post = Yii::$app->request->post();
    $post=['company_name'=>'123','mobile_phone'=>12,'sender_dep'=>'2323'];
    if ($post) {
        $model->login_id = $session['login_id'];
        $model->case_id  = "1";
        $model->ems_id   = "2";
        $model->create_time = date('Y-m-d H:i:s',time());
        $model->setAttributes($post);
        if($model->save()){
            return "保存成功!";
        }
        return "保存失败!";
    }
    return "保存失败!";
}

$post value failed to save

Solution:

Two problems:

1,

Yii::$app->response->format=Response::FORMAT_JSON; This sentence is used when returning json and should be removed here.

2. Your data may not pass the model's verification rules. You can print the verification results before save().


The above is the detailed content of What should I do if yii save cannot save data?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:When did yii2.0 come out?Next article:When did yii2.0 come out?