我有一个上下文,我有一个表格供用户在大会上进行注册。并且有两种不同的上下文:当“all_participant”为“1”时,当“all_participant”在 congresses 表中为“0”时。
当all_participant为“1”时:
- 如果 congress 表中的“all_participant”为“1”,并且用户在某些门票类型中进行注册并且没有门票类型与自定义问题相关联,则在注册表中只需要收集姓名和姓氏用户正在注册的每个参与者
- 如果“all_participant”为“1”,并且用户正在注册某些具有关联自定义问题的票证类型,则有必要为在该票证类型中注册的每个参与者收集该自定义问题的答案自定义问题
当“all_participants”在 congress 表中为 1 时更好:
当用户点击注册表中的“转到第 2 步”时,会在注册表中插入一个条目,在参与者表中为每个参与者插入一个条目,并在答案表中插入与自定义问题的答案相关的条目。因此,当用户在注册表单中单击“转到第 2 步”时,数据库将保持如下状态:
Registrations table:
id status congress_id main_participant_id
7 C 1 1
Participants table:
id registration_id ticket_type_id name surname
12 7 1 John W
13 7 2 Jake Y
Answers table:
id participant_id question_id answer
2 12 1 0002
3 13 1 0003
当all_participant为“0”时:
我的疑问是当“all_participants”为“0”时如何存储信息。因此,正在进行注册的用户 John 选择了 2 张票,一张票类型为“tt1”,另一张票类型为“tt2”,票类型“tt1”关联了 1 个自定义问题,现在“all_participants”是“0”表示不需要收集每个参与者的信息,只需要使用auth用户的信息进行注册。
但是,如果有自定义问题,auth 用户(正在进行注册的用户)必须回答这些自定义问题,但是如果“all_participant”为“0”,则只有进行注册的用户需要回答这些问题问题,例如,如果用户选择了两张票,并且 1 张或更多张有一些相关的自定义问题,在注册表中,除了用户在注册表中选择了 2 张票之外,它只会出现一次自定义问题,而不是两次因为仅供正在注册的用户回答(因为“all_participants”为“0”)。因此,在这种情况下,当用户在注册表单中单击“转到第 2 步”时,数据库保持如下状态:
Registrations table:
id status congress_id main_participant_id
10 C 1 1
Participants table: (name and surname and blank because when "all_participant" is "0" is not necessary to collect name and surname of each participant)
id registration_id ticket_type_id name surname
18 10 1
19 10 2
Answers table:
id participant_id question_id answer
4 18 1 0002
疑问:
我怀疑你是否知道这个结构是否正确,因为看起来不可能知道答案属于哪个用户 当“all_participant”为“0”且用户选择的一种或多种工单类型中存在自定义问题时。因为 ansers 表只有 participant_id,在本例中为“18”,但进行注册的用户是用户表中 ID 为“1”的用户。
registrations 表中的 main_participant_id 是 users 表中进行注册的用户的 id,可以知道是哪个用户进行了注册。
为了更好地说明“all_participant”为 0 时的 3 种可能情况:
与问题相关的关系:
1 to many between Congresses and Registrations
1 to many between Congresses and TicketTypes
1 to many between Registrations and Participants
1 to many between TicketTypes and Participants
1 to many between Participants and Answers
1 to many between Questions and Answers
Many to Many between TicketTypes and Questions
1 to many between Congresses and Questions
问题的相关模型:
// Congress model
class Congress extends Model
{
// A conference has one creator
public function creator(){
return $this->belongsTo('App\User', 'user_id');
}
public function ticketTypes(){
return $this->hasMany('App\TicketType', 'congress_id');
}
public function registrations(){
return $this->hasMany('App\Registration', 'congress_id');
}
}
// User model
class User extends Authenticatable
{
public function congresses(){
return $this->hasMany('App\Congress', 'user_id');
}
// A user can register in many conferences
public function registrations(){
return $this->hasMany('App\Registration','main_participant_id');
}
}
// Registration model
class Registration extends Model
{
// a registration has one user that do the registration
public function customer(){
return $this->belongsTo('App\User');
}
// a registration can have many participants
public function participants(){
return $this->hasMany('App\Participant');
}
public function congress(){
return $this->belongsTo('App\Congress');
}
}
// Participant Model
class Participant extends Model
{
// a participant belongs to a registration
public function registration(){
return $this->belongsTo('App\Registration');
}
}
// Ticket Type model
class TicketType extends Model
{
public function congress(){
return $this->belongsTo('App\Congress');
}
public function questions(){
return $this->belongsToMany('App\Question', 'ticket_type_questions')->withPivot(['required']);;
}
}
// Question model
class Question extends Model
{
public function ticket_type(){
return $this->belongsToMany('App\TicketType', 'ticket_type_questions')
->withPivot('required');
}
}
// Answer model
class Answer extends Model
{
public function question(){
return $this->belongsTo('Question');
}
public function participant(){
return $this->belongsTo('Participant');
}
}
// TicketTypeQuestion model
class RegistrationTypeQuestion extends Model
{
}
要注册用户和他可以注册的其他参与者,在两种情况下:“all_participant”为“1”和“all_participant”为“0”,我现在有 register() 方法,例如:
public function register(Request $request, $id, $slug = null, Validator $validator){
$allParticipants = Congress::where('id', $id)->first()->all_participants;
$user = Auth::user();
$rules = [];
$messages = [];
if(isset($request->participant_question_required)) {
$messages = [
'participant_question.*.required' => 'Fill all mandatory fields',
'participant_name.*.required' => 'Fill all name fields.',
'participant_surname.*.required' => 'Fill all surname fields.',
];
foreach ($request->participant_question_required as $key => $value) {
$rule = 'string|max:255';
if ($value) {
$rule = 'required|' . $rule;
}
$rules["participant_question.{$key}"] = $rule;
}
}
if($allParticipants){
$rules["participant_name.*"] = 'required|max:255|string';
$rules["participant_surname.*"] = 'required|max:255|string';
}
$validator = Validator::make($request->all(), $rules, $messages);
$errors = $validator->errors();
$errors = json_decode($errors);
if($validator->fails()) {
return response()->json([
'success' => false,
'errors' => $errors
], 422);
}
if($validator->passes()) {
$registration = Registration::create([
'congress_id' => $id,
'main_participant_id' => $user->id,
'status' => 'C',
]);
$participants = [];
for ($i = 0; $i < count($request->participant_name); $i++) {
$name = ($allParticipants) ? $request->participant_name[$i] : '';
$surname = ($allParticipants) ? $request->participant_surname[$i] : '';
$participants[] = Participant::create([
'name' => $name,
'surname' => $surname,
'registration_id' => $registration->id,
'ticket_type_id' => $request->ttypes[$i]
]);
}
if (isset($request->participant_question))
for ($i = 0; $i < count($request->participant_question); $i++)
$answer = Answer::create([
'question_id' => $request->participant_question_id[$i],
'participant_id' => $participants[$i]->id,
'answer' => $request->participant_question[$i],
]);
}
return response()->json([
'success' => true,
'message' => 'success'
], 200);
}
最佳答案
在我看来,您的 Answers 表需要了解回答者的行为能力。请看我的评论,就像你描述情况一样透彻,我在某些事情上还是有点不清楚。但目前,我的直觉是您的数据结构将“主要”或“参与者”的概念误解为一个独特的人,而不是给定人可能承担的角色。
我认为您需要的是如下所示的表格:
Table | Relationship | Table
-------------------------------------------------
Users | one-to-many | Participants
Groups | one-to-many | Participants
Participants | many-to-many | Roles
| (Participant_Roles) |
Answers | one-to-one | Participant_Roles (replace your Answers.participant_id with this)
Answers | one-to-one | Question
Tickets | many-to-many | Question
| (Ticket_Questions) |
您的角色可能是“主要”和“与会者”之类的东西。所以这里的诀窍是让您的答案不仅引用这个人,还引用他们当时的行为。
关于php - 当 "all_participant"是 "0"时,数据库的结构是否正确以处理这种情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50125462/