当"all_participant"为"0"时,数据库是否正确构造以处理场景?

7 php database database-design laravel

我有一个上下文,我有一个表格供用户在国会注册.并且有两种不同的上下文:当"all_participant"为"1"且"cong_participant"为"0"时,会议表中.

当all_participant为"1"时:

  • 如果会议桌中的"all_participant"为"1",并且用户正在某些故障单类型中进行注册而且没有任何故障单类型都有关联的自定义问题,则需要在注册表中仅收集有关每个参与者的姓名和姓氏.用户正在注册
  • 如果"all_participant"为"1"并且用户正在某些票证类型中进行注册,则需要关联自定义问题,以便为具有该自定义问题的票证类型中注册的每个参与者收集该自定义问题的答案

为了更好地在议会表中"all_participants"为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
Run Code Online (Sandbox Code Playgroud)

当all_participant为"0"时:

我怀疑的是当"all_participants"为"0"时如何存储信息.因此,正在进行注册的用户John选择了2张票,一张票类型为"tt1"的票证和其他票证类型"tt2",并且票证类型"tt1"具有1个自定义问题,现在"all_participants"是"0",这意味着没有必要收集有关每个参与者的信息,只需要使用auth用户的信息进行注册.

但是如果需要自定义问题,auth用户(正在进行注册的用户)回答这些自定义问题,但如果"all_participant"为"0",则只有正在进行注册的用户需要回答这些问题,所以例如,如果用户选择了两张票,而且一张或多张有一些相关的自定义问题,则在注册表中,除了用户在注册表中选择了两张票之外,它只会出现一次自定义问题,而不是两次,因为只是对于正在进行注册的用户应答(因为"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
Run Code Online (Sandbox Code Playgroud)

怀疑:

我怀疑的是,如果您知道这是否结构正确,因为 "all_participant"为"0" ,似乎无法知道答案属于哪个用户, 并且有一个或多个故障单类型中的自定义问题由用户.因为ansers表只有participant_id,在这种情况下是"18",但是进行注册的用户是user表中id为"1"的用户.

注册表中的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
Run Code Online (Sandbox Code Playgroud)

该问题的相关模型:

// 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
{

}
Run Code Online (Sandbox Code Playgroud)

要注册用户和他可以注册的其他参与者,在两种情况下:"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);
    }
Run Code Online (Sandbox Code Playgroud)

Cla*_*ore 2

在我看来,您的答案表需要了解回答者的行为能力。请看我的评论,虽然你描述的情况很彻底,但我对某些事情仍然有点不清楚。但目前,我的预感是,您的数据结构将“主要”或“参与者”的概念误解为一个独特的人,而不是某个特定人可能承担的角色。

我认为您需要的是如下所示的表格:

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)  |
Run Code Online (Sandbox Code Playgroud)

您的角色可能是“主要”和“与会者”之类的。因此,这里的技巧是让你的答案不仅参考这个人,还参考他们当时的行为方式。