Нужно добавить пользователя и чтобы много что не писать, использовал стандартный
bologer.ru/yii2-basic-registraciya-i-avtorizaciya-...
В регистрации нормально работает, а вот при создание пользователя, как-то не очень
Сама форма
<?php $form = ActiveForm::begin(['id' => 'form-signup']); ?>
<?= $form->field($model, 'username')->textInput(['autofocus' => true]) ?>
<?= $form->field($model, 'email') ?>
<?= $form->field($model, 'password')->passwordInput() ?>
<div class="form-group">
<?= Html::submitButton('Signup', ['class' => 'btn btn-primary', 'name' => 'signup-button']) ?>
</div>
<?php ActiveForm::end(); ?>
У меня в контроллере
use app\models\SignupForm;
...
public function actionCreate()
{
$model = new SignupForm();
if ($model->load(Yii::$app->request->post())) {
if ($user = $model->signup()) {
return $this->redirect(['index']);
}
}
return $this->render('create', [
'model' => $model,
]);
}
И модель SignupForm
<?php
namespace app\models;
use Yii;
use yii\base\Model;
/**
* Signup form
*/
class SignupForm extends Model
{
public $username;
public $email;
public $password;
/**
* @inheritdoc
*/
public function rules()
{
return [
['username', 'trim'],
['username', 'required'],
['username', 'unique', 'targetClass' => '\app\models\User', 'message' => 'This username has already been taken.'],
['username', 'string', 'min' => 2, 'max' => 255],
['email', 'trim'],
['email', 'required'],
['email', 'email'],
['email', 'string', 'max' => 255],
['email', 'unique', 'targetClass' => '\app\models\User', 'message' => 'This email address has already been taken.'],
['password', 'required'],
['password', 'string', 'min' => 6],
];
}
/**
* Signs user up.
*
* @return User|null the saved model or null if saving fails
*/
public function signup()
{
if (!$this->validate()) {
return null;
}
$user = new User();
$user->username = $this->username;
$user->email = $this->email;
$user->setPassword($this->password);
$user->generateAuthKey();
return $user->save() ? $user : null;
}
}
Если даже делать
var_dump($user->username)
То он показывает мне значение и оно не пустое, но в save его вообще нет, кроме password auth_key и прочее. В чем загвоздка тут может быть?
Значение которая выводит $user до
saveobject(app\models\User)[137]
public 'id' => null
public 'username' => string 'rusline2' (length=8)
public 'email' => string 'test@test.com' (length=13)
public 'created_at' => null
public 'auth_date' => null
private '_attributes' (yii\db\BaseActiveRecord) =>
array (size=2)
'password_hash' => string '$2y$13$g5S7bap.Zld602P9MDpY7.golGzMIGEsmSdBgUg5YV6b9zZ8PsxE6' (length=60)
'auth_key' => string 'ofJNRZmje1Fbe-oJedDNlmIBa-233OP7' (length=32)
private '_oldAttributes' (yii\db\BaseActiveRecord) => null
private '_related' (yii\db\BaseActiveRecord) =>
array (size=0)
empty
private '_relationsDependencies' (yii\db\BaseActiveRecord) =>
array (size=0)
empty
private '_errors' (yii\base\Model) => null
private '_validators' (yii\base\Model) => null
private '_scenario' (yii\base\Model) => string 'default' (length=7)
private '_events' (yii\base\Component) =>
array (size=2)
'beforeInsert' =>
array (size=1)
0 =>
array (size=2)
...
'beforeUpdate' =>
array (size=1)
0 =>
array (size=2)
...
private '_eventWildcards' (yii\base\Component) =>
array (size=0)
empty
private '_behaviors' (yii\base\Component) =>
array (size=1)
0 =>
object(yii\behaviors\TimestampBehavior)[136]
public 'createdAtAttribute' => string 'created_at' (length=10)
public 'updatedAtAttribute' => string 'updated_at' (length=10)
public 'value' => null
public 'attributes' =>
array (size=2)
...
public 'skipUpdateOnClean' => boolean true
public 'preserveNonEmptyValues' => boolean false
public 'owner' =>
&object(app\models\User)[137]