我之前问过类似的问题,但从那以后我做了很多改变,现在我有一个不同的问题。 我已将模式更改为如下所示。
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
#define MAXSTR 25
#define FORMAT_LEN 15
typedef struct person //structure that holds info about person
{
char name[MAXSTR + 1];
char surname[MAXSTR + 1];
}Guest;
typedef struct persons // list of persons
{
Guest person;
struct persons* next;
} Persons;
typedef struct //Pointer to first person in list
{
Persons* list;
} List;
typedef struct room //structure that holds info about rooms that are reserved
{
unsigned int ID;
unsigned int floor;
unsigned int bedcount;
struct tm dateArrival;
struct tm dateDep;
bool cancelled;
unsigned int price;
Guest* person;
List* list;
}Reservation;
typedef struct rooms
{
Reservation room;
struct rooms* next;
} Reservations;
bool PutGuestInsideList(List* list);
char* EnterString(char* str);
int main()
{
Reservations* room = NULL;
List list = { NULL };
printf("New guest: \n");
if(PutGuestInsideList(room->room.list) == false)
printf("Try again. \n");
}
bool PutGuestInsideList(List* list)
{
if(list == NULL)
return false;
Persons* newpers = (Persons*) malloc(sizeof(Persons));
if(newpers == NULL)
return false;
printf("Name: ");
EnterString(newpers->person.name);
printf("Surname: ");
EnterString(newpers->person.surname);
list->list = newpers;
return true;
}
char* EnterString(char* str)
{
if (str == NULL)
return str;
char format[FORMAT_LEN] = "";
sprintf(format, "%%%u[^\n]", MAXSTR);
scanf(format, str);
scanf("%*[^\n]");
scanf("%*c");
return str;
}
实质上,用户可以是许多user_group的一部分,而user_groups可以有很多用户。所以我正在寻找多对多的关系 这就是我添加表users_user_groups的原因。然后我为这两个设置我的模型
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('userName')->default('');
$table->string('userEmail')->default('');
$table->timestamps();
});
Schema::create('user_groups', function(Blueprint $table)
{
$table->increments('id');
$table->string('groupName')->default('');
$table->timestamps();
$table->softDeletes();
});
Schema::create('users_user_groups', function(Blueprint $table)
{
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('group_id')->unsigned();
});
Schema::table('users_user_groups', function(Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('group_id')->references('id')->on('user_groups');
});
我非常有信心上面的设置正确,对我来说这是有道理的。现在,这项工作将在我的UserController中进行。核心功能就是这个 我试图评论它来解释发生了什么。
class User extends Model
{
protected $table = 'users';
protected $guarded = [];
public function groups()
{
return $this->belongsToMany('App\Group', 'users_user_groups')->withPivot('user_id', 'group_id');
}
}
class Group extends Model
{
protected $table = 'user_groups';
protected $guarded = [];
public function profusionUser()
{
return $this->belongsToMany('App\User', 'pusers_user_groups')->withPivot('user_id', 'group_id');
}
}
所以我的user_groups表填充了类似的数据
public function updateUsers()
{
$users = Helper::returnUsersFromLdap(); //Get all users from Directory
foreach($users as $userName => $userData) {
$user = User::firstOrNew(['userName' => $userName]); //Create user if needed
foreach ($userData as $userEmail => $userDepartment) {
$name = preg_replace('/@.*?$/', '', $userEmail);
$user->userEmail = $userEmail;
$userGroups = Helper::returnGroupsFromLdap($name); //Get all user_groups user is apart off
foreach ($userGroups as $group) {
$usGroup = new Group(); //Create the user_groups
$usGroup->groupName = $group;
$usGroup->save();
}
$user->save();
$user->groups()->sync($userGroups); //Link the users groups to the user
}
}
return Redirect::route('users.index');
}
现在第一个问题是当我不确定他们是否需要时,在这个表中会重复群组?如果2个用户在同一个 组,上面将有两个条目,每个用户一个。我应该以某种方式使这个独特吗?
现在我的第二个问题是关于我的数据透视表。输出就像这样
+-----+------------+---------------------+---------------------+------------+
| id | groupName | created_at | updated_at | deleted_at |
+-----+------------+---------------------+---------------------+------------+
| 1 | Group 1 | 2016-05-20 15:55:34 | 2016-05-20 15:55:34 | NULL |
| 2 | Group 2 | 2016-05-20 15:55:34 | 2016-05-20 15:55:34 | NULL |
| 3 | Group 3 | 2016-05-20 15:55:34 | 2016-05-20 15:55:34 | NULL |
| 4 | Group 4 | 2016-05-20 15:55:34 | 2016-05-20 15:55:34 | NULL |
| 5 | Group 1 | 2016-05-20 15:55:34 | 2016-05-20 15:55:34 | NULL |
| 6 | Group 3 | 2016-05-20 15:55:34 | 2016-05-20 15:55:34 | NULL |
| 7 | Group 2 | 2016-05-20 15:55:34 | 2016-05-20 15:55:34 | NULL |
| 8 | Group 5 | 2016-05-20 15:55:34 | 2016-05-20 15:55:34 | NULL |
| 9 | Group 1 | 2016-05-20 15:55:34 | 2016-05-20 15:55:34 | NULL |
| 10 | Group 2 | 2016-05-20 15:55:34 | 2016-05-20 15:55:34 | NULL |
| 11 | Group 1 | 2016-05-20 15:55:34 | 2016-05-20 15:55:34 | NULL |
| 12 | Group 3 | 2016-05-20 15:55:34 | 2016-05-20 15:55:34 | NULL |
所以user_id正在正确更新,并且正确的次数。所以我知道id为1的用户分为3组。 但是,由于某种原因,group_id没有更新。
是否有任何原因未正确更新?任何建议表示赞赏,并获得有关user_groups的一些输入会很好 表以及是否应该包含唯一的组名。
非常感谢
答案 0 :(得分:1)
我不知道那是什么用的&#34;丰富&#34;表,但我认为:
Schema::create('users_user_groups', function(Blueprint $table)
{
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('group_id')->unsigned();
});
应该是这样的:
Schema::create('users_user_groups', function(Blueprint $table)
{
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('group_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('group_id')->references('id')->on('user_groups');
});
我的意思是,你必须指定&#34;外国&#34;部分在你的users_user_groups表中,但不知怎的,它最终出现在&#34; profusion_whatever&#34;表
答案 1 :(得分:1)
如果2个用户在同一个组中,则上面会有两个条目,每个用户一个。我应该以某种方式使这个独特吗?
是的,您的user_groups表应该具有唯一的组。您的数据透视表的工作是维持多对多关系。
在user_groups中有重复组的原因是创建组的foreach循环位于用户的foreach中。
不是在该循环中进行直接数据库插入,而是在包含组的foreach(用户)循环内构造一个数组。
https