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

WBOY
Lepaskan: 2016-06-06 20:32:25
asal
1692 orang telah melayarinya

<code>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");
        }
    }
</code>
Salin selepas log masuk
Salin selepas log masuk

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

<code>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'),
        );
    }
</code>
Salin selepas log masuk
Salin selepas log masuk

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

这是数据库
这是别人写的

回复内容:

<code>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");
        }
    }
</code>
Salin selepas log masuk
Salin selepas log masuk

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

<code>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'),
        );
    }
</code>
Salin selepas log masuk
Salin selepas log masuk

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

这是数据库
这是别人写的

<code>php</code><code>if ($model->save()) {
                    $this->redirect(array('user/apply'));
                }
</code>
Salin selepas log masuk

这一段我习惯写成

<code>php</code><code>if (!$model->save()) {
    dump($model->errors);
}
$this->redirect(['user/apply']);
</code>
Salin selepas log masuk

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

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan