yii2 - SQLSTATE[23000]:违反完整性约束:1048

发布时间:2021-02-25 08:29

我是 yii2 的新手,我正在尝试向数据库发送请求,但结果显示此类列不能为空,尽管通过 @var_dump 检查时我可以看到发送数据,这是怎么回事以及如何修复它

---> 控制器

 public function actionSignup()
{
    $model = new Signup();

    if (isset($_POST['Signup'])) {
        $model->attributes = Yii::$app->request->post('Signup');


        if ($model->validate()) {
            $model->signup();
            # code...
        }
    }

    return $this->render('signup', ['model'=>$model]);

---> 查看页面

<?php
use \yii\widgets\ActiveForm; 
?>
<?php
$form = ActiveForm::begin(['class'=>'form-horizontal']);
?>

<?= $form->field($model,'email')->textInput(['autofocus'=>true]) ?>

<?= $form->field($model,'password')->passwordInput() ?>

<div>
<button type="submit" class="btn primary-btn">Submit</button>
</div>
<?php
ActiveForm::end();
?>

---> 模型

class Signup extends Model
{
public $email;
public $password;

public function reles()
{
    return [

        [['email', 'password'], 'required'],
        ['email', 'email'],
        ['email', 'unique', 'targetClass'=>'app\models\User'],
        ['password', 'string', 'min'=>2,'max'=>10]
    ];
}

public function signup()
{
    $user = new User();
    $user->email = $this->email;
    $user->password = $this->password;
    return $user->save();
}

---> phpmyadmin 数据库

enter image description here

namespace app\models;

use yii\db\ActiveRecord;


class User extends ActiveRecord
{

}

--> var_dump enter image description here

--> sql enter image description hereenter image description here enter image description here

回答1

试试这个:

**
     * Signs user up.
     *
     * @return mixed
     */
    public function actionSignup()
    {

        $model = new SignupForm()
        //Sign up if it is post request
        if ($model->load(Yii::$app->request->post()) && $model->signup()) { //Here YOU LOAD your POST data in to the Lodal using load() method.
            Yii::$app->session->setFlash('success', 'Thank you for registration. Please check your email for further instructions.', false);
            return $this->goHome();
        } else {
            Yii::$app->session->setFlash('success', 'Sorry, you can not register right now.', false);
            return $this->redirect('/login');
        }
    }
回答2

在模型中

首先,属性规则方法叫做“规则”我不知道如果您在问题中打错了字,这就是问题所在;如果用户输入数据没有规则,返回值将是NULL,我以前遇到过这个问题,我没有包含rules()< /em> 方法(或者在您的情况下,它的名称不正确)并且模型为属性值返回 null。您的代码必须是:

public function rules()
{
    return [

        [['email', 'password'], 'required'],
        ['email', 'email'],
        ['email', 'unique', 'targetClass'=>'app\models\User'],
        ['password', 'string', 'min'=>2,'max'=>10]
    ];
}

技术说明:通常,或根据我的理解,密码应至少为 8 而不是 2 个字符,如果输入的密码只有 2 个字符,则可以一秒钟就被强制执行。

在控制器中

在@Serghei Leonenco 的回答中,Yii2 已经提供了一些用于处理 post 请求等的功能,所以请坚持使用 Yii2 功能,它可以增强您的应用程序安全

所以在 actionSignup() 方法中的代码应该是

public function actionSignup()
{
    $model = new Signup();

    // Loads the post request directly onto the model and validates it against the model rules() method
    if($model->load(Yii::$app->request->post()) && $model->validate){
            $model->signup();      
            // Model validated and saved successfully, IT OUGHT TO BE SAVED IN THE DATABASE AT THIS POINT
            // Here you can set a flash or redirect the user to another page  
    }

    return $this->render('signup', ['model'=>$model]);
}

在视图中

最后,在您的视图代码中,最好将 Yii2 内部函数用于 Html 按钮

<?= Html::submitButton('Signup', ['class' => 'btn btn-primary']) ?>

当然不要忘记使用 yii\helpers\Html

use yii\helpers\Html;