最佳 OOP 实践 PHP/MySQL

发布于 2024-11-03 20:28:01 字数 385 浏览 3 评论 0原文

我正在创建一个单词老师:

我创建了一个包含多个表的数据库:

TblTheme (themeId, name);<br>
TblGroup (groupId, name, themeIdFK);<br>
TblWeek (weekId, week, groupIdFK);<br>
tblWord (wordId, word, weekIdFK);<br>

所以 themeIdFK、groupIdFK 和 weekIdFK 是外键。

在 PHP 中我想显示所有主题。如果按下主题,则显示所有组等。 我应该创建什么类。 (如课程主题、小组、周和单词) 在不使数据库超载的情况下检索数据的最佳方法是什么?

I am creating a word teacher:

I created a database with multiple tables:

TblTheme (themeId, name);<br>
TblGroup (groupId, name, themeIdFK);<br>
TblWeek (weekId, week, groupIdFK);<br>
tblWord (wordId, word, weekIdFK);<br>

So themeIdFK, groupIdFK and weekIdFK are Foreign keys.

In PHP i want to show all the themes. If a theme is pressed, show all the groups etc..
What classes should i create. (like classes Theme, group, week and word)
and what is the best way to retrieve the data, without overloading the database?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

So尛奶瓶 2024-11-10 20:28:01

首先,如果列包含相同的数据,那么它应该具有相同的名称:

Themes (theme_id, name);
Groups (group_id, name, theme_id);

然后当您创建 JOIN 时,您可以将其写为

SELECT 
   Themes.name AS theme,
   Groups.name AS group
FROM Themes 
LEFT JOIN Groups USING theme_id

至于您原来的问题:
我会选择课程:

  • 老师 - 这将是你的 MVC 模型
  • Word - 由模型使用,保存你的日常单词和一些有关它的附加信息
  • WordMapper - 负责存储和检索 Word 类的实例

基本上这个设置将实现 DataMapper 模式

为每个表创建一个类是没有意义的,因为附加表仅保存有关单词的信息。至少我是这么看的。

不过,我不太确定您是否需要制作一个复杂的应用程序,只是为了制作日常用语......也许只是作为练习。

First of all , if column holds same data , then it should have the same name:

Themes (theme_id, name);
Groups (group_id, name, theme_id);

Then when you create JOIN , you can write it as

SELECT 
   Themes.name AS theme,
   Groups.name AS group
FROM Themes 
LEFT JOIN Groups USING theme_id

As for you original question :
I would go with classes :

  • Teacher - that would be your MVC model
  • Word - used by model, holds you daily word and some additional info about it
  • WordMapper - responsible for storing and retrieving instances of Word class

Basically this setup would implement DataMapper pattern.

There is no point in making a class per table, because the additional tables are only holding info about the Words. At lest that's how i see it.

Though, I'm not so sure if you need to make a complex App, just for making word-of-day thing .. maybe only as exercise.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文