yii2mod / yii2-comments

Comments module for Yii2
MIT License
159 stars 64 forks source link

how to get and show avatar #53

Closed cobabikin closed 7 years ago

cobabikin commented 7 years ago

my model User related model UserProfile code models User

`public static function tableName() { return '{{%user}}'; }

public function attributeLabels() { return [ 'username' => Yii::t('common', 'Username'), 'email' => Yii::t('common', 'Email'), 'status' => Yii::t('common', 'Status'), 'created_at' => Yii::t('common', 'Created at'), 'updated_at' => Yii::t('common', 'Updated at'), 'action_at' => Yii::t('common', 'Last action at'), ]; }

public function getUserProfile() { return $this->hasOne(UserProfile::className(), ['user_id' => 'id']); }`

and code model UserProfile ` public static function tableName() { return '{{%user_profile}}'; }

public function behaviors()
{
    return [
        'uploadBehavior' => [
            'class' => UploadBehavior::className(),
            'attributes' => [
                'avatar_path' => [
                    'path' => '@storage/avatars',
                    'tempPath' => '@storage/tmp',
                    'url' => Yii::getAlias('@storageUrl/avatars'),
                ],
            ],
        ],
    ];
}

    public function attributeLabels()
{
    return [
        'firstname' => Yii::t('common', 'Firstname'),
        'lastname' => Yii::t('common', 'Lastname'),
        'birthday' => Yii::t('common', 'Birthday'),
        'avatar_path' => Yii::t('common', 'Avatar'),
        'gender' => Yii::t('common', 'Gender'),
        'website' => Yii::t('common', 'Website'),
        'other' => Yii::t('common', 'Other'),
    ];
}`
ihorchepurnyi commented 7 years ago

You need to create the following method in your model:

public function getAvatar()
{
    // your custom code
}