Adding custom functions to models and extension updates

Hey!

I’ve installed cinghie/yii2-user-extended (dektrium/yii2-user) and it’s working well but there’s a need to add custom functions to the User model (getGroup). What is the proper way to do this without losing all the changes after an extension update? Should I make a new model extending yii2-user?

Thanks!

Yes. Extension code should not be touched.

I did the following changes:

  1. in config\web model map ‘User’=> ‘app\models\User’ instead of the one to cinghie’s User

  2. app\models\User:


<?php


namespace app\models;

use Yii;

use cinghie\yii2userextended\models\User as BaseUser;

use yii\db\Query;

class User extends BaseUser

{

    public function getText()

    {

        return 'test text';

    }

}

but I can’t figure out how to reach it (calling unknown method)

Yii::$app->user->identity->getText() tries to get the method from dektrium\user\models\User::getText()

and

Yii::$app->user->getText() from yii\web\User::getText(), which should be the right place but still says the method is unknown.

You need to specify your own class in application config:




'user' => [

    'identityClass' => 'app\models\User',



It’s working now.

Figured if cinghie/yii2-user-extended didn’t require to change that then neither would my User.

I don’t yet know what or why’s there a difference between

Yii::$app->user->identity->getText()

and

Yii::$app->user->getText()

identity refers to the model given in the config identityClass but what’s just ‘user’ then?

Thanks a lot!

Because Yii::$app->user is /yii/web/User and Yii::$app->user->identity is what’s implementing IdentityInterface (typically your own user model).