Good afternoon, there is such a code in the controller:

public function actionRegistration() { if(Yii::$app->request->isAjax) { $usersModel = new UsersModel(); $usersModel -> setScenario('registration'); $usersModel -> setAttributes($_POST['UsersModel'], true); if($usersModel -> validate() && $usersModel -> registration()) { $usersModel -> id = $usersModel ->guid(); $usersModel -> login = $usersModel->email; $usersModel -> hash = md5(rand()) . '_' . md5(rand()); $usersModel -> save(false); return json_encode([200, Yii::t('yii', 'In a letter sent to your e-mail to confirm your email address. mail, please read it')]); } else { return json_encode([500, Yii::t('yii', 'incorrectly entered e-mail')]); } } else { throw new HttpException(404, Yii::t('yii', 'Page not found.')); } } 

It seems simple, but when you call save() throws an error:

 <pre>Exception (Database Exception) &#039;yii\db\Exception&#039; with message &#039;SQLSTATE[HY000]: General error: 1364 Field &#039;login&#039; doesn&#039;t have a default value The SQL being executed was: INSERT INTO `users` (`id`) VALUES (NULL)&#039; in C:\sergey\sites\bitask.com\vendor\yiisoft\yii2\db\Schema.php:534 

For some reason, the id field is assigned NULL, in yii 1.1 all the time I just called save (), and then the guys got a bit too smart. When calling var_dump ($ usersModel), it shows that all the fields are correctly filled in, especially id can’t get it, thanks in advance for the help.

    2 answers 2

    As for null:

    Most likely, guid() returns a value that either fails validation or is of a type other than defined in the users table schema.

    Regarding your error - MySQL says that the login field cannot be NULL. Since in the code I see the installation of a login in an email, you may be trying to save an entry in the registration() function and this field is empty there.

    PS: Instead of md5 it is better to use Yii::$app->security->generatePasswordHash

      Understood, this is all Yii1, I defined all the fields of the table in the form of public variables in the model, and in Yii2 this leads to such consequences, all variables are redefined to empty values. Just just learning. Special thanks for Yii::$app->security->generatePasswordHash