/** * Add a group_id to the list of _groups to which this User belongs, checking that the Group exists and the User isn't already a member. * * This method does NOT modify the database. Call `store` to persist to database. * @param int $group_id The id of the group to add the user to. * @throws Exception The specified group does not exist. * @return User this User object. */ public function addGroup($group_id) { $this->getGroupIds(); // Return if user already in group if (in_array($group_id, $this->_groups)) { return $this; } // Next, check that the requested group actually exists if (!Group::find($group_id)) { throw new \Exception("The specified group_id ({$group_id}) does not exist."); } // Ok, add to the list of groups $this->_groups[] = $group_id; return $this; }