-
Notifications
You must be signed in to change notification settings - Fork 163
Profile and custom registration
webvimark edited this page Feb 22, 2015
·
4 revisions
Let's say you want to have profile for your users with following fields
name: string,
info: text,
stored in "user_profile" table.
- Create migration --
Do not forget to add foreign key for "user" table
<?php
use yii\db\Migration;
class m150219_211721_create_user_profile extends Migration
{
public function safeUp()
{
$tableOptions = null;
if ( $this->db->driverName === 'mysql' )
{
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB';
}
$this->createTable('user_profile', [
'id' => 'pk',
'user_id' => 'int',
'name' => 'string',
'info' => 'text',
'created_at' => 'int',
'updated_at' => 'int',
], $tableOptions);
$this->addForeignKey('fk_user_profile_user_id', 'user_profile', 'user_id', 'user', 'id', 'CASCADE', 'CASCADE');
}
public function safeDown()
{
$this->dropForeignKey('fk_user_profile_user_id', 'user_profile');
$this->dropTable('user_profile');
}
}
- Generate model via gii --
<?php
namespace app\models;
use webvimark\modules\UserManagement\models\User;
use Yii;
use yii\behaviors\TimestampBehavior;
/**
* This is the model class for table "user_profile".
*
* @property integer $id
* @property integer $user_id
* @property string $name
* @property string $info
* @property integer $created_at
* @property integer $updated_at
*
* @property User $user
*/
class UserProfile extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'user_profile';
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
TimestampBehavior::className(),
];
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['user_id'], 'integer'],
[['info'], 'string'],
[['name'], 'string', 'max' => 255],
[['name'], 'trim']
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'ID'),
'user_id' => Yii::t('app', 'User'),
'name' => Yii::t('app', 'Name'),
'info' => Yii::t('app', 'Info'),
'created_at' => Yii::t('app', 'Created'),
'updated_at' => Yii::t('app', 'Updated'),
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getUser()
{
return $this->hasOne(User::className(), ['id' => 'user_id']);
}
}
- Create custom RegistrationForm class --
Let's name it RegistrationFormWithProfile
<?php
namespace app\models;
use webvimark\modules\UserManagement\models\forms\RegistrationForm;
use webvimark\modules\UserManagement\models\User;
use webvimark\modules\UserManagement\UserManagementModule;
use yii\helpers\ArrayHelper;
use Yii;
class RegistrationFormWithProfile extends RegistrationForm
{
public $name;
public $info;
/**
* @return array
*/
public function rules()
{
return ArrayHelper::merge(parent::rules(), [
[['name', 'info'], 'required'],
[['info'], 'string'],
[['name'], 'string', 'max' => 255],
[['name', 'info'], 'trim'],
[['name', 'info'], 'purgeXSS'],
]);
}
/**
* @return array
*/
public function attributeLabels()
{
return ArrayHelper::merge(parent::attributeLabels(), [
'name'=>Yii::t('app', 'Name'),
'info'=>Yii::t('app', 'Info'),
]);
}
/**
* Look in parent class for details
*
* @param User $user
*/
protected function saveProfile($user)
{
$model = new UserProfile();
$model->user_id = $user->id;
$model->name = $this->name;
$model->info = $this->info;
$model->save(false);
}
}
- In module config set registrationFormClass --
'user-management' => [
...
'registrationFormClass' => 'app\models\RegistrationFormWithProfile',
...
],
- Create theme for registration view file --
<?php
use webvimark\modules\UserManagement\UserManagementModule;
use yii\bootstrap\ActiveForm;
use yii\captcha\Captcha;
use yii\helpers\Html;
/**
* @var yii\web\View $this
* @var webvimark\modules\UserManagement\models\forms\RegistrationForm $model
*/
$this->title = UserManagementModule::t('front', 'Registration');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="user-registration">
<h2 class="text-center"><?= $this->title ?></h2>
<?php $form = ActiveForm::begin([
'id'=>'user',
'layout'=>'horizontal',
'validateOnBlur'=>false,
]); ?>
<?= $form->field($model, 'username')->textInput(['maxlength' => 50, 'autocomplete'=>'off', 'autofocus'=>true]) ?>
<?= $form->field($model, 'password')->passwordInput(['maxlength' => 255, 'autocomplete'=>'off']) ?>
<?= $form->field($model, 'repeat_password')->passwordInput(['maxlength' => 255, 'autocomplete'=>'off']) ?>
<?= $form->field($model, 'name') ?>
<?= $form->field($model, 'info')->textarea(['rows'=>7]) ?>
<?= $form->field($model, 'captcha')->widget(Captcha::className(), [
'template' => '<div class="row"><div class="col-sm-2">{image}</div><div class="col-sm-3">{input}</div></div>',
'captchaAction'=>['/user-management/auth/captcha']
]) ?>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-9">
<?= Html::submitButton(
'<span class="glyphicon glyphicon-ok"></span> ' . UserManagementModule::t('front', 'Register'),
['class' => 'btn btn-primary']
) ?>
</div>
</div>
<?php ActiveForm::end(); ?>
</div>
- Done :) --