Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom profile and FAQ ;) #10

Closed
glanisko opened this issue Jan 30, 2015 · 25 comments
Closed

custom profile and FAQ ;) #10

glanisko opened this issue Jan 30, 2015 · 25 comments

Comments

@glanisko
Copy link

I know it is in FAQ, but after I have read the instructions - I could not do it. I have created user_profile table and ... I now get stuck.
Could you add more code how to create custom profile fields without changing module?

@webvimark
Copy link
Owner

Basically you create some "profile" table witch has "user_id" as foreign key to "user" table. And the you just run gii generators.

P.S. I know that FAQ is needed, but right now I have not so much spare time :)

@webvimark
Copy link
Owner

OK, the basic example:

user profile table with collumns:

id
user_id
age
pet_name

  1. Generate model UserProfile via gii somewhere in your app.
  2. Generate crud for this model via gii
  3. Now you can add data to this table. Lets say it will looks like this:

id => 1
user_id => 37
age => 8
pet_name => "Doggy"

Thats it, you now have some additional profile information for user with id "37"

@glanisko
Copy link
Author

glanisko commented Feb 3, 2015

ok, that's the place i know ;)
how merge this information to registration form, save them with user save on user-management/user/create ? ;)

@webvimark webvimark reopened this Feb 3, 2015
@webvimark
Copy link
Owner

For registration

  1. In config file you can redefine "registrationFormClass" property, so you need create your own registration from class (just copy-paste existsing and modify it for your needs)

  2. Use theming for registration view file

  3. ....

  4. Profit ? :)

@webvimark
Copy link
Owner

If it's not helping, I'll create example tomorrow

@glanisko
Copy link
Author

glanisko commented Feb 3, 2015

ok so:

  • i have created table: user_profile
class m150130_190352_create_user_profile extends Migration
{
    public function up()
    {
        $this->createTable('user_profile', [
            'user_id' => 'pk',
            'name' => Schema::TYPE_STRING . ' NOT NULL',
            'number' => Schema::TYPE_STRING . ' NOT NULL',
        ]);
        $this->addForeignKey('fk_user_id', 'user_profile', 'user_id', 'user', 'id');   
    }
}
  • generated model and crud controller
  • added to conmponents config section
'UserManagmentModule' => [
    'class' => 'webvimark\modules\UserManagement\UserManagementModule',
    'registrationFormClass' => 'app\models\UserProfile',
],
  • copied view file to my repository
use webvimark\modules\UserManagement\models\User;
use webvimark\modules\UserManagement\UserManagementModule;
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use webvimark\extensions\BootstrapSwitch\BootstrapSwitch;
/**
 * @var yii\web\View $this
 * @var webvimark\modules\UserManagement\models\User $model
 * @var yii\bootstrap\ActiveForm $form
 */
?>
<div class="user-form">
    <?php $form = ActiveForm::begin([
        'id'=>'user',
        'layout'=>'horizontal',
        'validateOnBlur' => false,
    ]); ?>
    <?= $form->field($model->loadDefaultValues(), 'status')
        ->dropDownList(User::getStatusList()) ?>
    <?= $form->field($model, 'username')->textInput(['maxlength' => 255, 'autocomplete'=>'off']) ?>
    <?php if ( $model->isNewRecord ): ?>
        <?= $form->field($model, 'password')->passwordInput(['maxlength' => 255, 'autocomplete'=>'off']) ?>
        <?= $form->field($model, 'repeat_password')->passwordInput(['maxlength' => 255, 'autocomplete'=>'off']) ?>
    <?php endif; ?>
    <?php if ( User::hasPermission('bindUserToIp') ): ?>
        <?= $form->field($model, 'bind_to_ip')
            ->textInput(['maxlength' => 255])
            ->hint(UserManagementModule::t('back','For example: 123.34.56.78, 168.111.192.12')) ?>
    <?php endif; ?>
    <?php if ( User::hasPermission('editUserEmail') ): ?>
        <?= $form->field($model, 'email')->textInput(['maxlength' => 255]) ?>
        <?= $form->field($model, 'email_confirmed')->checkbox() ?>
    <?php endif; ?>
    <div class="form-group">
        <div class="col-sm-offset-3 col-sm-9">
            <?php if ( $model->isNewRecord ): ?>
                <?= Html::submitButton(
                    '<span class="glyphicon glyphicon-plus-sign"></span> ' . UserManagementModule::t('back', 'Create'),
                    ['class' => 'btn btn-success']
                ) ?>
            <?php else: ?>
                <?= Html::submitButton(
                    '<span class="glyphicon glyphicon-ok"></span> ' . UserManagementModule::t('back', 'Save'),
                    ['class' => 'btn btn-primary']
                ) ?>
            <?php endif; ?>
        </div>
    </div>
    <?php ActiveForm::end(); ?>
</div>
  • added two fileds to copied file in step 4
    <?= $form->field($model, 'name')->passwordInput(['maxlength' => 255, 'autocomplete'=>'off']) ?>
    <?= $form->field($model, 'number')->passwordInput(['maxlength' => 255, 'autocomplete'=>'off']) ?>
  • get Unknown Property Exception ;)
Getting unknown property: webvimark\modules\UserManagement\models\User::name

and now what to do? :D

@webvimark
Copy link
Owner

Awww, man it's easier for me to make example.
You do it wrong :0

@glanisko
Copy link
Author

glanisko commented Feb 3, 2015

ok, so i will wait for your example :)

@glanisko
Copy link
Author

ok, when the example will be done?;)

@webvimark
Copy link
Owner

Have no time right now :( Probably will do it till Wednesday.

Here is the start - https://github.com/webvimark/user-management/wiki/Profile-and-custom-registration

@glanisko
Copy link
Author

Can you manage to do it till Sunday? :) it's blocking issue in my project :(

@webvimark
Copy link
Owner

Ok, will do it tomorrow

@webvimark
Copy link
Owner

@webvimark
Copy link
Owner

Sorry, I just noticed that I didn't upload last update to github (so example will not work) :(
Will do it when I'll be at home

@glanisko
Copy link
Author

OK :) i will wait :)

@webvimark
Copy link
Owner

Aaaand now it should work :)

@glanisko
Copy link
Author

Ok, now it works :) thanks :)

@ik-j
Copy link

ik-j commented May 19, 2015

I want the User table to have a custom field i.e. school_id. I added to table, then to views and to webvimark\modules\UserManagement\models\User model class, but its value is not saved/updated from your extension. I know I can use profile but I would rather prefer it via User table. Please guide me.

@webvimark
Copy link
Owner

Try add this field to rules

@ik-j
Copy link

ik-j commented May 19, 2015

Actually i hv 3 custome fields which I have added to rules n User class i.e. [['name', 'phone','school_id'],'required'],

@webvimark
Copy link
Owner

Try

        echo "<pre>";
        var_dump($model->errors);
        echo "</pre>";

And see what errors you have

@glanisko
Copy link
Author

I have done it with something like this:
config/web.php

'components' => [
        'user' => [
            'class' => 'webvimark\modules\UserManagement\components\UserConfig',
            'identityClass' => 'app\models\User',
        ],
        'view' => [
            'theme' => [
                'pathMap' => [
                    '@vendor/webvimark/module-user-management/views' => '@app/views/webvimark/module-user-management/',
                    '@vendor/webvimark/module-user-management/views/layouts/' => '@app/views/layouts/',
                ],
            ],
        ],
]

'modules'=>[
        'user-management' => [
            'class' => 'webvimark\modules\UserManagement\UserManagementModule',
            'controllerMap' => [
                'user' => 'app\controllers\UserController',
            ],
        ],
    ],

and app\models\User.php

<?php

namespace app\models;
use webvimark\modules\UserManagement\models\User as BaseUser;
use yii;
/**
* @property string $name
* @property string $surname
* @property string $telephone
*/
class User extends BaseUser
{

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return ArrayHelper::merge(parent::rules(), [
            [['name', 'surname'], 'required'],
            [['name', 'surname', 'telephone'], 'string', 'max' => 255],
                   ]);
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return ArrayHelper::merge(parent::attributeLabels(), [
            'name' => Yii::t('app', 'Name'),
            'surname' => Yii::t('app', 'Surname'),
            'telephone'=> Yii::t('app', 'Telephone number'),
        ]);
    }    
}

and override user controller in this way:

<?php

namespace app\controllers;
use webvimark\modules\UserManagement\controllers\UserController as BaseUserController;
use Yii;
use yii\helpers\Url;

class UserController extends BaseUserController
{

    public $modelClass = 'app\models\User';
    public $modelSearchClass = 'app\models\UserSearch';

}

and it works without touching vendor module ;)

@ik-j
Copy link

ik-j commented May 25, 2015

@glanisko, thanks buddy....your solution worked but only while updating a user. During creation [name,phone,school_id] doesnt get inserted into table :(

@ik-j
Copy link

ik-j commented May 25, 2015

I was able to solve by making the following 2 changes in Webvimark User class:

  1. Added properties at the start of the class:
  1. Added to rules():
    [['name','phone','school_id'], 'required'],

@ik-j
Copy link

ik-j commented Mar 18, 2016

I have a Yii2 app where I want the registered and logged in user to edit his user table custom fields like name, company_name etc.

I created a view "my-account.php" inside vendor->webvimark->module-user-management->views/auth/ folder. I added an action fuction (actionMyAccount() ) in webvimark->module-user-management->controller->authController class.

I am able to pull and show user custom columns data on view but am unable to update that data. Help needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants