yii - 在 yii2.0 框架中插入时出错

标签 yii

在 yii2.0 中将表单插入数据库时​​,我总是收到不同的错误。我遇到的最新错误是“Unknown Property – yii\base\UnknownPropertyException”-Getting unknown property: app\models\UserForm::subject。我已经发布了我的模型、 Controller 、 View 文件。请有人帮助我。谢谢你

Controller

<?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\UserForm;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use app\models\Form;

class UserController extends Controller
{
public function behaviors()
{
    return [
        'access' => [
            'class' => AccessControl::className(),
            'only' => ['logout'],
            'rules' => [
                [
                    'actions' => ['logout'],
                    'allow' => true,
                    'roles' => ['@'],
                ],
            ],
        ],
      /*  'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'logout' => ['post'],
            ],
        ], */
    ];
}
public function actions()
{
    return [
        'error' => [
            'class' => 'yii\web\ErrorAction',
        ],
        'captcha' => [
            'class' => 'yii\captcha\CaptchaAction',
            'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
        ],
    ];
}
public function actionUser()
{
    $model = new UserForm();                                                     
    if ($model->user(Yii::$app->request->post())) {            
        Yii::$app->session->setFlash('contactFormSubmitted');

        return $this->refresh();
    } else {
        return $this->render('user', [
            'model' => $model,
        ]);
    }
}
}

下面是我的 型号 文件
<?php
namespace app\models;

use Yii;
use yii\base\Model;

use yii\db\ActiveRecord;
/**
 * ContactForm is the model behind the contact form.
 */
class UserForm extends yii\db\ActiveRecord
{
public $name;
public $email;
const STATUS_INACTIVE = 0;
const STATUS_ACTIVE = 1;

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

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'],
    ];
}
/**
 * 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 user($email)
{
    //$model->name = $ContactForm['name'];
/*  if(!empty($UserForm)){ 
        $model->name = $UserForm['UserForm']['name'];
        $model->email = $UserForm['UserForm']['email'];



           Yii::$app->mailer->compose()
                ->setTo($email)
                ->setFrom([$this->email => $this->name])
                ->setSubject($this->subject)
                ->setTextBody($this->body)
                ->send(); */
                if ($this->validate()) {
                $form = new Form();
                $form->name = $this->name;
                $form->email = $this->email;
                $form->save();


            return true;
        } else {
            return false;
        }
    }
    }

    ?>

下面是我的 查看 文件
    <?php
    use yii\helpers\Html;
    use yii\bootstrap\ActiveForm;
    use yii\captcha\Captcha;

    /* @var $this yii\web\View */
    /* @var $form yii\bootstrap\ActiveForm */
    /* @var $model app\models\ContactForm */

    $this->title = 'User';
    $this->params['breadcrumbs'][] = $this->title;
    ?>
    <div class="site-contact">
    <h1><?= Html::encode($this->title) ?></h1>

    <?php if (Yii::$app->session->hasFlash('contactFormSubmitted')): ?>

    <div class="alert alert-success">
    Thank you for contacting us. We will respond to you as soon as possible.
    </div>

    <p>
    Note that if you turn on the Yii debugger, you should be able
    to view the mail message on the mail panel of the debugger.
    <?php if (Yii::$app->mailer->useFileTransport): ?>
    Because the application is in development mode, the email is not sent but saved as
    a file under <code><?= Yii::getAlias(Yii::$app->mailer->fileTransportPath) ?></code>.
    Please configure the <code>useFileTransport</code> property of the <code>mail</code>
    application component to be false to enable email sending.
    <?php endif; ?>
    </p>

    <?php else: ?>

    <p>
    If you have business inquiries or other questions, please fill out the following form to contact us. Thank you.
    </p>

    <div class="row">
    <div class="col-lg-5">
        <?php $form = ActiveForm::begin(['id' => 'contact-form']); ?>
            <?= $form->field($model, 'name') ?>
            <?= $form->field($model, 'email') ?>
            <div class="form-group">
                <?= Html::submitButton('Submit', ['class' => 'btn btn-primary', 'name' => 'contact-button']) ?>
            </div>
        <?php ActiveForm::end(); ?>
    </div>
     </div>

    <?php endif; ?>
    </div>

最佳答案

在您的模型中,我没有看到字段 subject但这在验证规则中提到。看起来你的模型和你的规则不一致。您正在扩展和事件记录 UserForm但是你的 Controller 如果为 User .如果您在模型(姓名、电子邮件)中声明字段,则这些字段不在数据库中。检查数据库和模型之间的一致性

关于yii - 在 yii2.0 框架中插入时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31586615/

相关文章:

php - JQuery - 复选框未打开

javascript - 地理定位 API - 等待获取位置

php - Yii CMenu Widget - 如何获得主页

ajax - 如何在 Yii 中调用另一个 Action 中的 Action ?

php - 使用 yii php 框架保持数据库版本控制的最佳方法是什么?

php - 执行自定义命令时出现 yii 错误

php - Backbone Sync 返回一个空的 $_POST 数组

PHP 的 "use"关键字未正确包含

javascript - Yii 通知用户有关违反约束的情况

php - Yii 功能测试错误