mpociot / teamwork

User to Team associations with invitation system for the Laravel 5 Framework
MIT License
1.07k stars 170 forks source link

When set current_team_id just create entry in team_user pivot table #3

Closed ziming closed 9 years ago

ziming commented 9 years ago

Hi,

I think it may be good if by doing $user->current_team_id = 5;

An appropriate record in team_user pivot is created if it previously did not exist.

mpociot commented 9 years ago

Don't know if this is a good idea to use the properties for this.

I'd prefer to do this with the Teamwork helper methods.

So I see two possible solutions:

First: Chain attach team and switch team (not working right now!)

$user->attachTeam( $newTeam )->switchTeam( $newTeam );

Second: Give the attachTeam method a second parameter to determine if the team should be automatically set as the current team. (Defaults to false)

$user->attachTeam( $newTeam, true )

I'd personally prefer the second approach.

Thoughts?

ziming commented 9 years ago

I think I prefer the second approach. Though for me the main reason I ask this question is because for my app, 1 user can only be in 1 team. Hence I thought doing that that would allow me to avoid making attach and detach calls.

I tried just $user->switchTeam($currentTeamId) but I got a user not in team exception

mpociot commented 9 years ago

Yep, the switchTeam method makes a check to see if the user is allowed to switch to this team.

Okay, then I'd make a change that the detachTeam method can be chainable.

So it would become:

$user->detachTeam( $user->currentTeam )->attachTeam( $newTeam );

This way the attachTeam method doesn't need a second parameter, because if current_team_id is NULL it will be set automatically.

mpociot commented 9 years ago

Done!

ziming commented 9 years ago

Thank you.