Skip to content

Commit

Permalink
Fixed problem with foreign column of team in users table
Browse files Browse the repository at this point in the history
  • Loading branch information
oliuz committed Sep 23, 2020
1 parent 7f27047 commit 6f4a688
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
10 changes: 10 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@
*/
'users_table' => 'users',

/*
|--------------------------------------------------------------------------
| Teamwork current team id in users Table
|--------------------------------------------------------------------------
|
| This is the current team id in users table name used by Teamwork.
|
*/
'current_team' => 'current_team_id',

/*
|--------------------------------------------------------------------------
| Teamwork Team Model
Expand Down
16 changes: 10 additions & 6 deletions src/Traits/UserHasTeams.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function teams()
*/
public function currentTeam()
{
return $this->hasOne(Config::get('teamwork.team_model'), 'id', 'current_team');
return $this->hasOne(Config::get('teamwork.team_model'), 'id', config('teamwork.current_team'));
}

/**
Expand Down Expand Up @@ -139,12 +139,13 @@ public function isOwnerOfTeam($team)
public function attachTeam($team, $pivotData = [])
{
$team = $this->retrieveTeamId($team);
$current_team_id = config('teamwork.current_team');
/**
* If the user has no current team,
* use the attached one
*/
if (is_null($this->current_team)) {
$this->current_team = $team;
if (is_null($this->$current_team_id)) {
$this->$current_team_id = $team;
$this->save();

if ($this->relationLoaded('currentTeam')) {
Expand Down Expand Up @@ -176,6 +177,7 @@ public function attachTeam($team, $pivotData = [])
*/
public function detachTeam($team)
{
$current_team_id = config('teamwork.current_team');
$team = $this->retrieveTeamId($team);
$this->teams()->detach($team);

Expand All @@ -189,8 +191,8 @@ public function detachTeam($team)
* If the user has no more teams,
* unset the current_team_id
*/
if ($this->teams()->count() === 0 || $this->current_team === $team) {
$this->current_team = null;
if ($this->teams()->count() === 0 || $this->$current_team_id === $team) {
$this->$current_team_id = null;
$this->save();

if ($this->relationLoaded('currentTeam')) {
Expand Down Expand Up @@ -239,6 +241,8 @@ public function detachTeams($teams)
*/
public function switchTeam($team)
{
$current_team_id = config('teamwork.current_team');

if ($team !== 0 && $team !== null) {
$team = $this->retrieveTeamId($team);
$teamModel = Config::get('teamwork.team_model');
Expand All @@ -254,7 +258,7 @@ public function switchTeam($team)
throw $exception;
}
}
$this->current_team = $team;
$this->$current_team_id = $team;
$this->save();

if ($this->relationLoaded('currentTeam')) {
Expand Down

0 comments on commit 6f4a688

Please sign in to comment.