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

时间:2015-04-24 05:13:13

标签: forms activerecord view model yii2

我已经使用Yii2完成了Web应用程序,因为提交的值不会保存在数据库中。我只是使用了模型+控制器+视图结构。实际上表单是在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(); ?>

1 个答案:

答案 0 :(得分:1)

从你给出的代码看来,你似乎没有将你的模型与数据库绑定。

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

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

此外,您不必手动将每个值分配给模型。大规模的任务为你做到了。