guild/common/models/ManagerEmployee.php

92 lines
2.3 KiB
PHP
Raw Permalink Normal View History

2021-11-16 13:14:28 +03:00
<?php
namespace common\models;
use yii\db\ActiveQuery;
/**
* This is the model class for table "manager_employee".
*
* @property int $id
* @property int $manager_id
2023-04-20 02:07:19 +03:00
* @property int $employee_id
2021-11-16 13:14:28 +03:00
*
* @property Manager $manager
* @property UserCard $userCard
2021-11-16 13:14:28 +03:00
*/
class ManagerEmployee extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'manager_employee';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
2023-04-20 02:07:19 +03:00
[['manager_id', 'employee_id'], 'required'],
[['manager_id'], 'integer'],
2023-05-04 15:10:56 +03:00
['employee_id', 'unique', 'targetAttribute' => ['manager_id', 'employee_id'], 'message' => 'Этот сотрудник уже закреплён за менеджером'],
[['employee_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['employee_id' => 'id']],
2021-11-16 13:14:28 +03:00
[['manager_id'], 'exist', 'skipOnError' => true, 'targetClass' => Manager::className(), 'targetAttribute' => ['manager_id' => 'id']],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'manager_id' => 'Менеджер',
2023-04-20 02:07:19 +03:00
'employee_id' => 'Карточка работника',
2021-11-16 13:14:28 +03:00
];
}
2023-05-04 01:18:24 +03:00
public function fields(): array
{
return [
'id',
'manager_id',
'employee_id',
2023-05-23 14:05:09 +03:00
'user_id' => function(){
return $this->employee_id;
},
2023-05-04 01:18:24 +03:00
'employee' => function () {
return [
"fio" => $this->employee->userCard->fio ?? $this->employee->username,
"avatar" => $this->employee->userCard->photo ?? '',
];
},
];
}
2021-11-16 13:14:28 +03:00
/**
* @return ActiveQuery
*/
2023-04-20 02:07:19 +03:00
public function getEmployee()
2021-11-16 13:14:28 +03:00
{
2023-04-20 02:07:19 +03:00
return $this->hasOne(User::class, ['id' => 'employee_id']);
2021-11-16 13:14:28 +03:00
}
2023-05-23 14:05:09 +03:00
public function getUser()
{
return $this->hasOne(User::class, ['id' => 'employee_id']);
}
2021-11-16 13:14:28 +03:00
/**
* @return ActiveQuery
*/
public function getManager()
{
return $this->hasOne(Manager::className(), ['id' => 'manager_id']);
}
}