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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user