简体   繁体   English

需要MySQL数据库结构建议。 从一张桌子到另外两个桌子一对多的两个多对多关系

[英]MySQL database structure advice needed. Two many-to-many from one table to two other that are in one-to-many relation

I have this setup as my database structure which already works pretty well, but I feel like it could be better, but cannot figure out how. 我已经将此设置作为数据库结构,它已经可以很好地工作了,但是我觉得它可能会更好,但无法弄清楚该如何做。

Database Structure:- 数据库结构:
数据库结构

Events can have multiple sub-events. 事件可以具有多个子事件。 Users can join these events and optionally their sub-events. 用户可以加入这些事件及其子事件(可选)。 With this structure I have all constraints, but the one that links the event attending with the sub event attendings => there can be sub event attendings left without having the main event attending (this shouldn't be possible as users who don't attend the main event cannot attend it's sub events). 通过这种结构,我具有所有约束,但是将活动参加者与子活动参加者联系起来的约束=>在没有主活动参加者的情况下可以剩下子活动参加者(这对于没有参加活动的用户是不可能的)主要活动无法参加其子活动)。

I am currently working with Laravel so it looks like this: 我目前正在与Laravel合作,因此它看起来像这样:

User : 用户名

  • hasMany Event (as organizer of these events) hasMany事件(这些事件的组织者)
  • belongsToMany EventAttending (which events is a user joining) ownersToMany EventAttending(哪些事件是用户加入的事件)
  • belongsToMany SubEventAttending (which sub-events is a user joining) ownersToMany SubEventAttending(哪些子事件是用户加入的)

Event : 事件

  • belongsTo User 属于用户
  • hasMany SubEvent hasMany子事件
  • belongsToMany EventAttending (which users are attending this event) ownersToMany EventAttending(哪些用户正在参加此事件)

SubEvent : 子事件

  • belongsTo Event 属于事件
  • belongsToMany SubEventAttending (which users are attending this sub-event) ownersToMany SubEventAttending(哪些用户正在参加此子事件)

The problem arises when trying to manage the sub-event attendings of a user. 当尝试管理用户的子事件出席时会出现问题。 How would I make this constraint between the event and sub-event attendings exist, while keeping it clean with the capabilities of Laravel / What can you give as advice for a better structuring / How would you guys do it? 我如何在活动和子活动参加之间建立这种约束,同时保持与Laravel的功能保持一致/您可以提供一些建议以更好地组织活动/你们将如何做?

Edit 编辑

To clarify, the events and sub-events store different things, have different properties. 为了澄清,事件和子事件存储不同的事物,具有不同的属性。 Also I need to store information for the event attendings (like did he attend in real life) and sub-event attendings, most likely differing ones. 另外,我还需要存储有关事件出席(如他在现实生活中参加过)和子事件出席的信息,很可能是不同的信息。

Edit 编辑

Edward Haber has the better structure, but as my two pivot tables (the connection between User-Event and User-SubEvent) store additional diferent type of information, currently chose to remain with the initial design. 爱德华·哈伯(Edward Haber)具有更好的结构,但是由于我的两个数据透视表(User-Event和User-SubEvent之间的连接)存储了其他不同类型的信息,因此目前选择保留初始设计。

One of the biggest problems I am facing (which does exist with both structure) is querying for a User while getting the attended Events with the attended SubEvents . 我面临的最大问题之一(这两种结构都存在)是在获取具有伴随的SubEvents的伴随事件的同时查询用户 Trying to achive this result: 尝试达到以下结果:

User[]:{  
  ...,  
    attending_events[]:{  
      ...,  
       attending_sub_events[]:{  
         ...  
       }  
    }  
}

Been thinking for hours for a clean solution, couldn't get anything else to work. 想了好几个小时才能找到一个干净的解决方案,却别无他法。 Wouldn't like to write the whole SQL query manually. 不想手动编写整个SQL查询。 My current implementation with the two pivot table looks like this (result not nested, code messy): 我目前使用两个数据透视表的实现如下所示(结果未嵌套,代码混乱):

$users = User::withCount(['attendingEvents' => function($query) use($eventId){
            $query->where('event_id', $eventId);
        }])
        ->with(['attendingSubEvents' => function($query) use($eventId){
            $query->select('id')->whereHas('event', function($query) use($eventId){
                $query->where('id', $eventId);
            });
        }]);

With this approach I get the sub-events separated from the main event. 通过这种方法,我将子事件与主事件分开。 (querying only for the main attending-event count, because I only need to determine whether he is joining or not). (仅查询主要的出席事件计数,因为我仅需要确定他是否正在加入)。

The pattern for this type of solution is to use a Polymorphic Relationship . 这种类型的解决方案的模式是使用多态关系 This solution focuses just on connecting the User to the Event and SubEvent - the rest of the relations look correct. 该解决方案仅专注于将用户连接到事件和子事件-其余的关系看起来正确。

<?php
class User extends Model {
    public function attending() {
        return $this->morphTo();
    }
}

<?php 
class Event extends Model {
    public function attendees() {
        return $this->morphMany('App\User', 'attending')
    }
}


<?php 
class Subevent extends Model {
    public function attendees() {
        return $this->morphMany('App\User', 'attending')
    }
}

The users table needs these fields: 用户表需要以下字段:

*users*
-----
id
attending_id
attending_type

This is all untested - but the basic idea. 这都是未经测试的-但基本思想。 (Edited: changed name of polymorphic method to attending from attendee) (编辑:将多态方法的名称更改为参加者出席)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM