Yii2 表单提交数据值未保存在数据库中

Yii2 form submit data values is not saving in database

我使用 Yii2 完成了 web 应用程序,表单提交的值没有保存在数据库中。我刚刚使用模型+控制器+视图 structure.Actually 表单正在 Mysql 数据库中提交,但是当我访问数据库时,我只能看到所有条目的空白值

SiteController.php

<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use app\models\CanForm;
use app\models\UploadForm;

use yii\web\UploadedFile;

class SiteController extends Controller
{
    public function actionContact()
    {
        $model = new ContactForm();
        if ($model->load(Yii::$app->request->post())) {

          $model->name = $_POST['ContactForm']['name'];
          $model->email = $_POST['ContactForm']['email'];
          $model->subject = $_POST['ContactForm']['subject'];
          $model->body = $_POST['ContactForm']['body'];

           if ($model->save())
            Yii::$app->response->redirect(array('site/contact', 'model' => $model));
            return $this->refresh();
        } else {
            return $this->render('contact', [
                'model' => $model,
            ]);
        }
    }
}
}

Model:ContactForm.php


namespace app\models;

use Yii;
use yii\base\Model;

use yii\db\ActiveRecord;
/**
 * ContactForm is the model behind the contact form.
 */
class ContactForm extends \yii\db\ActiveRecord
{
    public $name;
    public $email;
    public $subject;
    public $body;
    public $verifyCode;

    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            // name, email, subject and body are required
            [['name', 'email', 'subject', 'body'], 'required'],
            // email has to be a valid email address
            ['email', 'email'],
            // verifyCode needs to be entered correctly
            ['verifyCode', 'captcha'],
        ];
    }

    /**
     * @return array customized attribute labels
     */
    public function attributeLabels()
    {
        return [
            'verifyCode' => 'Verification Code',
        ];
    }

    /**
     * Sends an email to the specified email address using the information collected by this model.
     * @param  string  $email the target email address
     * @return boolean whether the model passes validation
     */
    public function contact($email)
    {
        if ($this->validate()) {
            Yii::$app->mailer->compose()
                ->setTo($email)
                ->setFrom([$this->email => $this->name])
                ->setSubject($this->subject)
                ->setTextBody($this->body)
                ->send();

            return true;
        } else {
            return false;
        }
    }
}
View:contact.php


    <div class="row">
        <div class="col-lg-5">
            <?php $form = ActiveForm::begin(['id' => 'contact-form']); ?>
                <?= $form->field($model, 'name') ?>
                <?= $form->field($model, 'email') ?>
                <?= $form->field($model, 'subject') ?>
                <?= $form->field($model, 'body')->textArea(['rows' => 6]) ?>
                <?= $form->field($model, 'verifyCode')->widget(Captcha::className(), [
                    'template' => '<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-6">{input}</div></div>',
                ]) ?>
                <div class="form-group">
                    <?= Html::submitButton('Submit', ['class' => 'btn btn-primary', 'name' => 'contact-button']) ?>
                </div>
            <?php ActiveForm::end(); ?>

从您提供的代码来看,您似乎没有将模型与数据库绑定。

将此功能添加到您的模型中:

public static function tableName()
{
    return 'your_table_name';
}

此外,您不必手动将每个值分配给模型。大量作业会为您做到这一点。