87 lines
2.6 KiB
PHP
87 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace common\components\user\models;
|
|
|
|
use Yii;
|
|
use yii\db\ActiveRecord;
|
|
use yii\behaviors\AttributeBehavior;
|
|
|
|
/**
|
|
* This is the model class for table "{{%user_admin}}".
|
|
*
|
|
* @property int $id
|
|
* @property int $user_id
|
|
* @property string $first_name
|
|
* @property string|null $last_name
|
|
* @property string|null $middle_name
|
|
* @property string|null $full_name
|
|
* @property int|null $birthday
|
|
* @property string|null $avatar
|
|
* @property string|null $location
|
|
* @property string|null $phone
|
|
* @property string|null $telegram
|
|
*
|
|
* @property User $user
|
|
*/
|
|
class UserAdmin extends ActiveRecord
|
|
{
|
|
public static function tableName(): string
|
|
{
|
|
return '{{%user_admin}}';
|
|
}
|
|
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
[['user_id', 'first_name'], 'required'],
|
|
[['user_id', 'birthday'], 'integer'],
|
|
[['first_name', 'last_name', 'middle_name', 'full_name', 'avatar', 'location', 'phone', 'telegram'], 'string', 'max' => 255],
|
|
[['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::class, 'targetAttribute' => ['user_id' => 'id']],
|
|
];
|
|
}
|
|
|
|
public function attributeLabels(): array
|
|
{
|
|
return [
|
|
'id' => Yii::t('user', 'ID'),
|
|
'user_id' => Yii::t('user', 'Пользователь'),
|
|
'first_name' => Yii::t('user', 'Имя'),
|
|
'last_name' => Yii::t('user', 'Фамилия'),
|
|
'middle_name' => Yii::t('user', 'Отчество'),
|
|
'full_name' => Yii::t('user', 'Полное имя'),
|
|
'birthday' => Yii::t('user', 'День рождения'),
|
|
'avatar' => Yii::t('user', 'Аватар'),
|
|
'location' => Yii::t('user', 'Локация'),
|
|
'phone' => Yii::t('user', 'Телефон'),
|
|
'telegram' => Yii::t('user', 'Телеграм'),
|
|
];
|
|
}
|
|
|
|
public function getUser(): \yii\db\ActiveQuery
|
|
{
|
|
return $this->hasOne(User::class, ['id' => 'user_id']);
|
|
}
|
|
|
|
public function getFullName(): string
|
|
{
|
|
return implode(' ', [$this->last_name, $this->first_name, $this->middle_name]);
|
|
}
|
|
|
|
// todo refactor
|
|
public function behaviors(): array
|
|
{
|
|
return [
|
|
[
|
|
'class' => AttributeBehavior::class,
|
|
'attributes' => [
|
|
ActiveRecord::EVENT_BEFORE_INSERT => 'full_name',
|
|
ActiveRecord::EVENT_BEFORE_UPDATE => 'full_name',
|
|
],
|
|
'value' => function ($event) {
|
|
return self::getFullName();
|
|
},
|
|
],
|
|
];
|
|
}
|
|
}
|