lundi 23 mai 2016

Linking pivot table for many to many

I make a call the following call

$users = Helper::returnUsersFromLdap();

This returns all users in my system in the following format

array:64 [▼
  "Some User" => array:1 [▼
    "someone@someone.com" => "Director"
  ]
  "Another User" => array:1 [▼
    "someone@someone.com" => "Assistant"
  ]
  ...
]

I then loop these users and if they are not in my database I add them

foreach($users as $userName => $userData) {

    $user = User::firstOrNew(['userName' => $userName]);

    foreach ($userData as $userEmail => $userDepartment) {
        $user->userEmail = $userEmail;
        $user->active = true;
        $user->save();
    }
}

So that is how I create my users. Now within the above loop, I then make the following call

$userGroups = Helper::returnGroupsFromLdap($userName);

This will return all the groups that user is apart off. I then loop this to get all the unique groups

foreach ($userGroups as $group) {
    if(!array_key_exists($group, $groupsArray)){
        $groupsArray[$group] = true;
    }
}

I then perform another loop to create the groups

foreach($groupsArray as $group => $key){
    if(!empty($group)) {
        $usGroup = Group::firstOrNew(['groupName' => $group]);
        $usGroup->groupName = $group;
        $usGroup->save();
    }
    $user->groups()->sync($groupsArray);
}

The complete code looks like the following

public function updateUsers()
{
    $users = Helper::returnUsersFromLdap();
    $groupsArray[] = array();

    DB::table('users')->update(array('active' => false));
    foreach($users as $userName => $userData) {

        $user = User::firstOrNew(['userName' => $userName]);

        foreach ($userData as $userEmail => $userDepartment) {
            $user->userEmail = $userName;
            $user->active = true;
            $user->save();
        }

        $userGroups = Helper::returnGroupsFromLdap($name);

        foreach ($userGroups as $group) {
            if(!array_key_exists($group, $groupsArray)){
                $groupsArray[$group] = true;
            }
        }

        foreach($groupsArray as $group => $key){
            if(!empty($group)) {
                $usGroup = Group::firstOrNew(['groupName' => $group]);
                $usGroup->groupName = $group;
                $usGroup->save();
            }
            $user->groups()->sync($groupsArray);
        }
    }
}

This produces a unique list of users within my users table, and a unique list of groups within my groups table. I also have a third table called users_user_groups. This is to act as a pivot table so I can store all groups a user is apart off. My User model is like so

class User extends Model
{
    protected $table = 'users';
    protected $guarded = [];

    public function group()
    {
        return $this->belongsToMany('App\Group', 'users_user_groups')->withPivot('user_id', 'group_id');
    }
}

And my Group model

class Group extends Model
{
    protected $table = 'user_groups';
    protected $guarded = [];

    public function user()
    {
        return $this->belongsToMany('App\User', 'users_user_groups')->withPivot('user_id', 'group_id');
    }
}

My users_user_groups table contains a user_id and a group_id. With the code I posted above, data is being stored within this table using the sync function. However, the data is like so

+-----+-------------------+----------+
| id  |           user_id | group_id |
+-----+-------------------+----------+
|   1 |                 1 |        0 |
|   2 |                 1 |        1 |
|   3 |                 2 |        0 |
|   4 |                 2 |        1 |
|   5 |                 3 |        0 |
|   6 |                 3 |        1 |
|   7 |                 4 |        0 |
|   8 |                 4 |        1 |
|   9 |                 5 |        0 |

So it does not seem to show what groups a user is apart of, it just seems to repeat itself. Now that I have a unique list of users and a unique list of groups, how can I use the pivot table to show what groups a user is apart off?

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire