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

What should I do if yii save cannot save data?

藏色散人
Release: 2020-07-23 11:21:51
Original
3775 people have browsed it

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 "保存失败!";
}
Copy after login

$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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template