new-support.webeffector.ru/common/components/user/models/UserCustomer.php

83 lines
2.4 KiB
PHP
Raw Permalink Normal View History

2024-07-04 06:42:20 +03:00
<?php
namespace common\components\user\models;
use Yii;
use yii\db\ActiveRecord;
use yii\behaviors\AttributeBehavior;
/**
* This is the model class for table "{{%user_customer}}".
*
* @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 string|null $telegram
* @property string|null $location
* @property string|null $phone
*
* @property User $user
*/
class UserCustomer extends ActiveRecord
{
public static function tableName(): string
{
return '{{%user_customer}}';
}
public function rules(): array
{
return [
[['user_id', 'first_name'], 'required'],
[['user_id'], 'integer'],
[['first_name', 'last_name', 'middle_name', 'full_name', 'telegram', 'location', 'phone'], 'string', 'max' => 255],
[['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::class, 'targetAttribute' => ['user_id' => 'id']],
];
}
public function attributeLabels()
{
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', 'Полное имя'),
'telegram' => Yii::t('user', ' Телеграм'),
'location' => Yii::t('user', 'Локация'),
'phone' => 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();
},
],
];
}
}