dongtan2017 2018-05-02 00:08
浏览 60

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

I have a context where I have a form for a user do a registration in a congress. And there are two different contexts: when "all_participant" is "1" and when "all_participant" is "0" in the congresses table.

When all_participant is "1":

  • if "all_participant" is "1" in the congress table, and the user is doing a registration in some ticket types and none ticket type have custom questions associated, in the registration form is necessary to collect only the name and surname about each participant that the user is registering
  • if "all_participant" is "1" and the user is doing a registration in some ticket types that have custom questions associated is necessary to collect the answer to that custom questions for each participant that is being registered in that ticket types that have that custom questions

To better when "all_participants" is 1 in the congress table:

enter image description here

When the user click "Go to step 2" in the registration form, is inserted an entry in the Registrations table, an entry for each participant in Participants table and entries in the answers table relative to the answers to the custom questions. So the database stays like below when the user click in "Go to step 2" in the registration form:

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

When all_participant is "0":

My doubt is about how to store the information when "all_participants" is "0". So the user John that is doing the registration selected 2 tickets, one ticket of the ticket type "tt1" and other of the ticket type "tt2", and the ticket type "tt1" has 1 custom question associated and now "all_participants" is "0", which means that is not necessary to collect info about each participant, is only necessary to use the info of the auth user to do the registration.

But if there are custom questions is necessary that the auth user (the user that is doing the registration) answer these custom questions, but if "all_participant" is "0" only the user that is doing the registration needs to answer these questions, so for example if the user selected two tickets and 1 or more have some custom questions associated, in the registration form, besides the user selected 2 tickets in the registration form it will only appear once the custom question(s) and not twice because is only for the user that is doing the registration to answer (because "all_participants" is "0"). So in this scenario when user clicks in the "Go to step 2" in the registration form the database stays like:

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

Doubt:

My doubt is if you know if this is structured correctly because as it is it seems that is not possible to know to which user the answers belong when "all_participant" is "0" and there are custom questions in 1 or more ticket types selected by the user. Because the ansers table only have the participant_id which in this case is "18" but the user that did the registration is the user in the users table with id "1".

The main_participant_id in the registrations table is the id of the user in the users table that did the registration which allow to know which user did the registration.

To better illustrate the 3 possible scenarios when "all_participant" is 0:

enter image description here

Relationships relevant for the question:

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

Relevant models for the question:

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

}

To register the user and other participants that he can register, in both scenarios: "all_participant" as "1" and "all_participant" as "0", I have the register() method for now like :

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);
    }
  • 写回答

1条回答 默认 最新

  • duanliang8464 2018-05-10 15:21
    关注

    It seems to me like your Answers table needs an awareness of in what capacity the answer-er was acting. Please see my comment, as thorough as you were in describing the situation, I'm still a little unclear on some things. But at the moment, my hunch is that your data-structure is misinterpreting the idea of "main" or "participant" as a unique person as opposed to a role that a given person may carry.

    I think what you need are tables that look like this:

    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)  |
    

    Your roles might be things like "main" and "attendee". So the trick here would be giving your Answer a reference to not just the person, but in what capacity they were acting at the time.

    评论

报告相同问题?

悬赏问题

  • ¥15 我的数据无法存进链表里
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端