2021-11-23 13:13:19 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace common\models;
|
|
|
|
|
2021-11-24 13:05:12 +03:00
|
|
|
use yii\behaviors\TimestampBehavior;
|
2021-11-25 12:33:08 +03:00
|
|
|
use yii\db\ActiveQuery;
|
2021-12-20 17:49:11 +03:00
|
|
|
use yii\db\ActiveRecord;
|
2021-11-24 13:05:12 +03:00
|
|
|
use yii\db\Expression;
|
|
|
|
use yii\helpers\ArrayHelper;
|
2021-11-23 13:13:19 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the model class for table "task".
|
|
|
|
*
|
|
|
|
* @property int $id
|
|
|
|
* @property int $project_id
|
|
|
|
* @property string $title
|
|
|
|
* @property int $status
|
|
|
|
* @property string $created_at
|
|
|
|
* @property string $updated_at
|
2023-04-25 01:32:15 +03:00
|
|
|
* @property int $column_id
|
|
|
|
* @property int $user_id
|
2021-11-23 13:13:19 +03:00
|
|
|
* @property string $description
|
|
|
|
*
|
|
|
|
* @property Project $project
|
2021-12-20 17:49:11 +03:00
|
|
|
* @property UserCard $card
|
|
|
|
* @property UserCard $cardIdCreator
|
2023-01-18 15:30:38 +03:00
|
|
|
* @property ProjectTaskUser[] $taskUsers
|
2021-11-23 13:13:19 +03:00
|
|
|
*/
|
2023-01-18 15:30:38 +03:00
|
|
|
class ProjectTask extends ActiveRecord
|
2021-11-23 13:13:19 +03:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public static function tableName()
|
|
|
|
{
|
2023-01-18 15:30:38 +03:00
|
|
|
return 'project_task';
|
2021-11-23 13:13:19 +03:00
|
|
|
}
|
|
|
|
|
2021-11-24 13:05:12 +03:00
|
|
|
public function behaviors()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'class' => TimestampBehavior::class,
|
|
|
|
'createdAtAttribute' => 'created_at',
|
|
|
|
'updatedAtAttribute' => 'updated_at',
|
|
|
|
'value' => new Expression('NOW()'),
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-11-23 13:13:19 +03:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function rules()
|
|
|
|
{
|
|
|
|
return [
|
2023-04-25 01:32:15 +03:00
|
|
|
[['project_id', 'status', 'title', 'description',], 'required'],
|
|
|
|
[['project_id', 'status', 'column_id', 'user_id'], 'integer'],
|
2021-11-23 13:13:19 +03:00
|
|
|
[['created_at', 'updated_at'], 'safe'],
|
2023-04-25 01:32:15 +03:00
|
|
|
['title', 'unique', 'targetAttribute' => ['title', 'project_id'], 'message' => 'Такая задача уже создана'],
|
2021-11-23 13:13:19 +03:00
|
|
|
[['title'], 'string', 'max' => 255],
|
|
|
|
[['description'], 'string', 'max' => 500],
|
|
|
|
[['project_id'], 'exist', 'skipOnError' => true, 'targetClass' => Project::className(), 'targetAttribute' => ['project_id' => 'id']],
|
2023-04-25 01:32:15 +03:00
|
|
|
[['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']],
|
|
|
|
[['column_id'], 'exist', 'skipOnError' => true, 'targetClass' => ProjectColumn::className(), 'targetAttribute' => ['column_id' => 'id']],
|
2021-11-23 13:13:19 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function attributeLabels()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'id' => 'ID',
|
2021-11-24 13:05:12 +03:00
|
|
|
'project_id' => 'Проект',
|
|
|
|
'title' => 'Название задачи',
|
|
|
|
'status' => 'Статус',
|
|
|
|
'created_at' => 'Дата создания',
|
|
|
|
'updated_at' => 'Дата обновления',
|
|
|
|
'description' => 'Описание',
|
2023-04-25 01:32:15 +03:00
|
|
|
'user_id' => 'Создатель задачи',
|
|
|
|
'column_id' => 'Колонка',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function fields(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'id',
|
|
|
|
'project_id',
|
|
|
|
'title',
|
|
|
|
'created_at',
|
|
|
|
'updated_at',
|
|
|
|
'description',
|
|
|
|
'status',
|
|
|
|
'column_id',
|
|
|
|
'user_id',
|
|
|
|
'taskUsers',
|
2021-11-23 13:13:19 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-11-24 13:05:12 +03:00
|
|
|
public function beforeDelete()
|
|
|
|
{
|
2023-04-25 01:32:15 +03:00
|
|
|
foreach ($this->taskUsers as $taskUser) {
|
2021-11-24 13:05:12 +03:00
|
|
|
$taskUser->delete();
|
|
|
|
}
|
|
|
|
return parent::beforeDelete();
|
|
|
|
}
|
|
|
|
|
2021-11-23 13:13:19 +03:00
|
|
|
/**
|
2021-11-25 12:33:08 +03:00
|
|
|
* @return ActiveQuery
|
2021-11-23 13:13:19 +03:00
|
|
|
*/
|
|
|
|
public function getProject()
|
|
|
|
{
|
|
|
|
return $this->hasOne(Project::className(), ['id' => 'project_id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-11-25 12:33:08 +03:00
|
|
|
* @return ActiveQuery
|
2021-11-23 13:13:19 +03:00
|
|
|
*/
|
2021-12-06 15:13:31 +03:00
|
|
|
public function getUser()
|
2021-11-23 13:13:19 +03:00
|
|
|
{
|
2021-12-06 15:13:31 +03:00
|
|
|
return $this->hasOne(User::className(), ['id' => 'user_id']);
|
2021-11-23 13:13:19 +03:00
|
|
|
}
|
|
|
|
|
2023-04-25 01:32:15 +03:00
|
|
|
/**
|
|
|
|
* @return ActiveQuery
|
|
|
|
*/
|
|
|
|
public function getColumn(): ActiveQuery
|
2021-12-20 17:49:11 +03:00
|
|
|
{
|
2023-04-25 01:32:15 +03:00
|
|
|
return $this->hasOne(ProjectColumn::class, ['id' => 'column_id']);
|
2021-11-23 13:13:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-11-25 12:33:08 +03:00
|
|
|
* @return ActiveQuery
|
2021-11-23 13:13:19 +03:00
|
|
|
*/
|
|
|
|
public function getTaskUsers()
|
|
|
|
{
|
2023-01-18 15:30:38 +03:00
|
|
|
return $this->hasMany(ProjectTaskUser::className(), ['task_id' => 'id']);
|
2021-11-23 13:13:19 +03:00
|
|
|
}
|
2021-11-24 13:05:12 +03:00
|
|
|
|
|
|
|
public static function usersByTaskArr($task_id): array
|
|
|
|
{
|
|
|
|
return ArrayHelper::map(
|
|
|
|
self::find()->joinWith(['user', 'project'])->where(['project_id' => $task_id])->all(), 'id', 'user.username');
|
|
|
|
}
|
2021-11-23 13:13:19 +03:00
|
|
|
}
|