add multi-assignment to tasks and projects
This commit is contained in:
@ -2,6 +2,8 @@
|
||||
|
||||
namespace common\models;
|
||||
|
||||
use Exception;
|
||||
use Yii;
|
||||
use yii\db\ActiveQuery;
|
||||
use yii\helpers\ArrayHelper;
|
||||
|
||||
@ -9,12 +11,13 @@ use yii\helpers\ArrayHelper;
|
||||
* This is the model class for table "project_user".
|
||||
*
|
||||
* @property int $id
|
||||
* @property int $card_id
|
||||
* @property int $project_id
|
||||
* @property int $user_id
|
||||
*
|
||||
* @property Project $project
|
||||
* @property UserCard $card
|
||||
* @property User $user
|
||||
* @property Task[] $tasks
|
||||
* @property TaskUser[] $taskUsers
|
||||
*/
|
||||
class ProjectUser extends \yii\db\ActiveRecord
|
||||
@ -33,10 +36,11 @@ class ProjectUser extends \yii\db\ActiveRecord
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
[['project_id', 'user_id'], 'required'],
|
||||
['user_id', 'unique', 'targetAttribute' => ['user_id', 'project_id'], 'message'=>'Сотрудник уже занят на этом проекте'],
|
||||
[['project_id', 'user_id'], 'integer'],
|
||||
[['user_id', 'project_id'], 'required'],
|
||||
['user_id', 'unique', 'targetAttribute' => ['user_id', 'project_id'], 'message'=>'Сотрудник уже назначен на этот проект'],
|
||||
[['card_id', 'project_id', 'user_id'], 'integer'],
|
||||
[['project_id'], 'exist', 'skipOnError' => true, 'targetClass' => Project::className(), 'targetAttribute' => ['project_id' => 'id']],
|
||||
[['card_id'], 'exist', 'skipOnError' => true, 'targetClass' => UserCard::className(), 'targetAttribute' => ['card_id' => 'id']],
|
||||
[['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']],
|
||||
];
|
||||
}
|
||||
@ -48,7 +52,8 @@ class ProjectUser extends \yii\db\ActiveRecord
|
||||
{
|
||||
return [
|
||||
'id' => 'ID',
|
||||
'project_id' => 'Проект',
|
||||
'card_id' => 'Карточка',
|
||||
'project_id' => 'Project ID',
|
||||
'user_id' => 'Сотрудник',
|
||||
];
|
||||
}
|
||||
@ -64,17 +69,17 @@ class ProjectUser extends \yii\db\ActiveRecord
|
||||
/**
|
||||
* @return ActiveQuery
|
||||
*/
|
||||
public function getUser()
|
||||
public function getCard()
|
||||
{
|
||||
return $this->hasOne(User::className(), ['id' => 'user_id']);
|
||||
return $this->hasOne(UserCard::className(), ['id' => 'card_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ActiveQuery
|
||||
*/
|
||||
public function getCard()
|
||||
public function getUser()
|
||||
{
|
||||
return $this->hasOne(UserCard::className(), ['id_user' => 'user_id']);
|
||||
return $this->hasOne(User::className(), ['id' => 'user_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -112,4 +117,30 @@ class ProjectUser extends \yii\db\ActiveRecord
|
||||
return ArrayHelper::map(
|
||||
self::find()->joinWith(['tasksByProject', 'user'])->where(['task.id' => $task_id])->all(), 'id', 'user.username');
|
||||
}
|
||||
|
||||
public static function setUsersByCardId()
|
||||
{
|
||||
$projectUserModels = self::findAll(['user_id' => null]);
|
||||
|
||||
foreach ($projectUserModels as $projectUser)
|
||||
{
|
||||
$projectUser->user_id = UserCard::getUserIdByCardId($projectUser->card_id);
|
||||
if ($projectUser->user_id !== null) {
|
||||
$projectUser->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function setCardsByUsersId()
|
||||
{
|
||||
$projectUserModels = self::findAll(['card_id' => null]);
|
||||
|
||||
foreach ($projectUserModels as $projectUser)
|
||||
{
|
||||
$projectUser->card_id = UserCard::getCardIdByUserId($projectUser->user_id);
|
||||
if ($projectUser->card_id !== null) {
|
||||
$projectUser->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -31,8 +31,8 @@ class TaskUser extends \yii\db\ActiveRecord
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
[['task_id', 'project_user_id'], 'integer'],
|
||||
['project_user_id', 'unique', 'targetAttribute' => ['task_id', 'project_user_id'], 'message'=>'Этот сотрудник уже назначен на эту задачу'],
|
||||
[['task_id', 'project_user_id'], 'required'],
|
||||
['project_user_id', 'unique', 'targetAttribute' => ['task_id', 'project_user_id'], 'message'=>'Уже закреплён(ы) за задачей'],
|
||||
[['project_user_id'], 'exist', 'skipOnError' => true, 'targetClass' => ProjectUser::className(), 'targetAttribute' => ['project_user_id' => 'id']],
|
||||
[['task_id'], 'exist', 'skipOnError' => true, 'targetClass' => Task::className(), 'targetAttribute' => ['task_id' => 'id']],
|
||||
];
|
||||
|
@ -3,8 +3,11 @@
|
||||
namespace common\models;
|
||||
|
||||
use common\classes\Debug;
|
||||
use Exception;
|
||||
use phpDocumentor\Reflection\Types\This;
|
||||
use Yii;
|
||||
use yii\behaviors\TimestampBehavior;
|
||||
use yii\db\ActiveQuery;
|
||||
use yii\db\Expression;
|
||||
use yii\filters\AccessControl;
|
||||
use yii\helpers\ArrayHelper;
|
||||
@ -141,7 +144,7 @@ class UserCard extends \yii\db\ActiveRecord
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
* @return ActiveQuery
|
||||
*/
|
||||
public function getFieldsValues()
|
||||
{
|
||||
@ -149,7 +152,7 @@ class UserCard extends \yii\db\ActiveRecord
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
* @return ActiveQuery
|
||||
*/
|
||||
public function getProjectUsers()
|
||||
{
|
||||
@ -157,7 +160,7 @@ class UserCard extends \yii\db\ActiveRecord
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
* @return ActiveQuery
|
||||
*/
|
||||
public function getPosition()
|
||||
{
|
||||
@ -165,7 +168,7 @@ class UserCard extends \yii\db\ActiveRecord
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
* @return ActiveQuery
|
||||
*/
|
||||
public function getStatus0()
|
||||
{
|
||||
@ -173,9 +176,9 @@ class UserCard extends \yii\db\ActiveRecord
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
* @return ActiveQuery
|
||||
*/
|
||||
public function getAchievements(): \yii\db\ActiveQuery
|
||||
public function getAchievements(): ActiveQuery
|
||||
{
|
||||
return $this->hasMany(AchievementUserCard::class, ['user_card_id' => 'id'])->with('achievement');
|
||||
}
|
||||
@ -206,6 +209,19 @@ class UserCard extends \yii\db\ActiveRecord
|
||||
return ArrayHelper::map(Skill::find()->all(), 'id', 'name');
|
||||
}
|
||||
|
||||
public function getUser(): ActiveQuery
|
||||
{
|
||||
return $this->hasOne(User::class, ['id' => 'id_user']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function getIdByUserId($user_id)
|
||||
{
|
||||
return ArrayHelper::getValue(self::find()->where(['id_user' => $user_id])->one(), 'id');
|
||||
}
|
||||
|
||||
public static function getUserList()
|
||||
{
|
||||
return ArrayHelper::map(self::find()->all(), 'id', 'fio');
|
||||
@ -268,4 +284,23 @@ class UserCard extends \yii\db\ActiveRecord
|
||||
$user_card->id_user = $user_id;
|
||||
$user_card->save();
|
||||
}
|
||||
|
||||
public static function getUserIdByCardId ($card_id)
|
||||
{
|
||||
$userCard = self::findOne(['id' => $card_id]);
|
||||
if (empty($userCard)) {
|
||||
return null;
|
||||
}
|
||||
return $userCard['id_user'];
|
||||
}
|
||||
|
||||
public static function getCardIdByUserId ($user_id)
|
||||
{
|
||||
$userCard = self::findOne(['id_user' => $user_id]);
|
||||
if (empty($userCard)) {
|
||||
return null;
|
||||
}
|
||||
return $userCard['id'];
|
||||
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user