【发布时间】:2021-04-21 20:34:52
【问题描述】:
我的数据库布局出现问题。基本上,布局以以下方式呈现:
- Community
- - Each community has officers (Model `User`)
- - Each community has participants (Model `User`)
- Chat rooms (different section)
- - each chat room has participants (Model `User`)
- - each chat has officers, which are generated based on the participant's community's officers. This means: It's not hard-coded into a new table, instead, it should fetch the officers of the community, to which the participant (non-officer) belongs to.
我的奋斗目标是实现最后一部分,即“官员可以访问”。我现在的查询完全是一团糟,尤其是当前服务器的计划。考虑到代码很长,我会直截了当:
- Fetch chat rooms
- - fetch participants of each chat room
- - - fetch communities of each participant
- - - - fetch officers of each community
- - - - add these officers to a main array in the model
- - - - remove any duplicated officers
现在,您可以看到这是一种非常松散的获取结果的方式,并且会消耗大量性能。官员和非官员之间唯一相关的模型是community。
最好的方法是什么?
【问题讨论】:
-
了解
JOIN。 -
我确实考虑过这一点,但我认为使用 Eloquent 模型是“最佳实践”?我应该只在关系中进行原始查询吗?