当前位置:Gxlcms > PHP教程 > YII新手提问,$model->save()失败

YII新手提问,$model->save()失败

时间:2021-07-01 10:21:17 帮助过:5人阅读

public function actionTestApply()
    {
        if (!Yii::app()->user->isGuest) {
            $model = Apply::model()->findByPk($_POST['product_id']);
            if (isset($_POST['LEMONCMS_CSRF_TOKEN'])) {
                $model->test_text = $_POST['test_text'];
                $model->img = $_POST['img'];
                if ($model->save()) {
                    $this->redirect(array('user/apply'));
                }
            }
        } else {
            $this->render("error");
        }
    }

能打印出 $model->test_text $model->img

public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('user_id, product_id', 'required'),
            array('user_id, product_id', 'numerical', 'integerOnly'=>true),
            array('insert_time', 'safe'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id, user_id, insert_time, product_id', 'safe', 'on'=>'search'),
        );
    }

这是数据库
这是别人写的

回复内容:

public function actionTestApply()
    {
        if (!Yii::app()->user->isGuest) {
            $model = Apply::model()->findByPk($_POST['product_id']);
            if (isset($_POST['LEMONCMS_CSRF_TOKEN'])) {
                $model->test_text = $_POST['test_text'];
                $model->img = $_POST['img'];
                if ($model->save()) {
                    $this->redirect(array('user/apply'));
                }
            }
        } else {
            $this->render("error");
        }
    }

能打印出 $model->test_text $model->img

public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('user_id, product_id', 'required'),
            array('user_id, product_id', 'numerical', 'integerOnly'=>true),
            array('insert_time', 'safe'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id, user_id, insert_time, product_id', 'safe', 'on'=>'search'),
        );
    }

这是数据库
这是别人写的

phpif ($model->save()) {
                    $this->redirect(array('user/apply'));
                }

这一段我习惯写成

phpif (!$model->save()) {
    dump($model->errors);
}
$this->redirect(['user/apply']);

这样如果没有save成功就会打印出错误了

人气教程排行