数据库的结构正确,以便在" all_participant"是" 0"?

时间:2018-05-02 00:08:10

标签: php database laravel database-design

我有一个上下文,我有一个表格供用户在国会注册。并且有两种不同的背景:当" all_participant"是" 1"当" all_participant"是" 0"在大会议席。

当all_participant为" 1":

  • if" all_participant"是" 1"在会议表中,并且用户正在某些票证类型中进行注册,并且没有票证类型具有关联的自定义问题,在注册表单中仅需要收集关于用户正在注册的每个参与者的姓名和姓氏
  • if" all_participant"是" 1"并且用户正在某些票证类型中进行注册,这些票证类型需要关联自定义问题,以便为具有该自定义问题的票证类型中注册的每个参与者收集该自定义问题的答案

更好的时候" all_participants"在会议桌上是1:

enter image description here

当用户点击"转到步骤2"在注册表中,在注册表中插入一个条目,参与者表中的每个参与者的条目和相对于自定义问题的答案的答案表中的条目。因此当用户点击&#34时,数据库保持如下;转到步骤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"只有正在进行注册的用户才需要回答这些问题,例如,如果用户选择了两张门票且一张或多张有一些相关的自定义问题,则在注册表格中,除了用户在注册表格中选择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"但是,进行注册的用户是user表中的用户,ID为" 1"。

注册表中的main_participant_id是进行注册的users表中用户的id,允许知道哪个用户进行了注册。

为了更好地说明" all_participant"是0:

enter image description here

与问题相关的关系:

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" as" 1"和#34; all_participant" as" 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);
    }

1 个答案:

答案 0 :(得分:2)

在我看来,您的答案表需要了解答案员的行为能力。请看我的评论,就像你描述情况一样彻底,我对某些事情仍然有点不清楚。但目前,我的预感是你的数据结构误解了“主要”或“参与者”作为一个独特的人的想法,而不是某个人可能带来的角色。

我认为你需要的是看起来像这样的表:

ImageService.Instance.LoadUrl(item.profileImg)
       .DownSample()
       .BitmapOptimizations(true)
       .LoadingPlaceholder("blank_profile_img.png", FFImageLoading.Work.ImageSource.CompiledResource)
       .WithCache(FFImageLoading.Cache.CacheType.Memory)
       .Into(holder.imgIcon);

您的角色可能是“主要”和“与会者”之类的内容。所以这里的诀窍就是让你的答案不仅提到这个人,而且还指他们当时的行为能力。