Just started with Yii2 and have (another) question I want to be able to create new users with the backend. I tried moving the signup.php and change some things but it didn’t work. Problems getting the model from for instance the SiteController to the “view” signup.php…
public function actionSignup()
{
$model = new User();
return $this->render('signup', [
'model' => $model,
]);
}
I then started with gii to create a new model and a CRUD. I changed the "form_.php"
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model backend\models\User */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="user-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'username')->textInput(['maxlength' => 255]) ?>
<?= $form->field($model, 'email')->textInput(['maxlength' => 255]) ?>
<?= $form->field($model, 'password')->passwordInput() ?>
<?= $form->field($model, 'status')->textInput() ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
and my user model:
<?php
namespace backend\models;
use Yii;
/**
-
This is the model class for table "user".
-
@property integer $id
-
@property string $username
-
@property string $auth_key
-
@property string $password_hash
-
@property string $password_reset_token
-
@property string $email
-
@property integer $status
-
@property integer $created_at
-
@property integer $updated_at
*/
class User extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'user';
}
public function rules()
{
return [
['username', 'filter', 'filter' => 'trim'],
['username', 'required'],
['username', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This username has already been taken.'],
['username', 'string', 'min' => 2, 'max' => 255],
['email', 'filter', 'filter' => 'trim'],
['email', 'required'],
['email', 'email'],
['email', 'unique', 'targetClass' => '\common\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()) {
$user = new User();
$user->username = $this->username;
$user->email = $this->email;
$user->setPassword($this->password);
$user->generateAuthKey();
if ($user->save()) {
return $user;
}
}
return null;
}
}
?>
But when I go to my application I get this error:
Getting unknown property: backend\models\User::password
It’s a bit hard to figure out when your just starting…