如何用3个PrimaryKey编写文件yml多对多表

发布于 2024-12-13 10:57:24 字数 511 浏览 1 评论 0原文

如果我们的情况是标准视图,您有两个表和一个三元,并且可以通过遵循 以下文档

但是,如果我们有三个表而不是两个表,那么三元组由 3 个 PrimaryKey 组成,正如我所写我的 yml 文件?

情况示例:

以我有一位参加课程的用户为例。 所以我有一个users表、courses表和一个users_has_courses(useri_id,course_id)。 这是多对多的标准情况。 但我还有一个表“invoices”,因此还有一个表“users_courses_invoices”,其中有三个主键(user_id、course_id、invoice_id)。

if our situation is the standard view where you have two tables and a ternary, and easily manageable by following the following documentation

But if we have three instead of two tables and then the ternary consists of 3 PrimaryKey, as I write my yml files?

Example to situation:

Take the case that I have a user who participates in a course.
So I have a users table, courses table and a users_has_courses (useri_id, course_id).
This is the standard case many-to-many.
But I also have a table invoices and therefore a table users_courses_invoices where there are three primaykey (user_id, course_id, invoice_id).

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

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

发布评论

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

评论(1

北斗星光 2024-12-20 10:57:24

在您的情况下,您有 UserCourse 模型,它们与具有一对 (user_id, course_id) 作为关键。我将调用此模型订阅,并为其提供自己的标识符,然后我将将此模型与 Invoice 模型链接起来,因此您的最终方案(最小版本)可能是

User:
  columns:
    id:
      type: integer
      primary: true

Course:
  columns:
    id:
      type: integer
      primary: true

Subscription:
  columns:
    id:
      type: integer
      primary: true
    user_id:
      type: integer
      notnull: true
    course_id:
      type: integer
      notnull: true
  relations:
    User:
      foreignAlias: Subscriptions
    Course:
      foreignAlias: Subscriptions

Invoice:
  columns:
    id:
      type: integer
      primary: true
    subscription_id:
      type: integer
      notnull: true
  relations:
    Subscription:
      foreignAlias: Subscription
      foreignType: One

:标准化数据库,您可以使用以下代码访问用户和课程的发票:

$user->Subscriptions->getFirst()->Invoice
$course->Subscriptions->getFirst()->Invoice

如果您想要给定用户的所有发票,您可以通过这种方式进行查询

InvoiceTable::getInstance()->createQuery('i')
    ->select('i.*')
    ->leftJoin('i.Subscription s')->leftJoin('s.User u')
    ->where('u.id = ?', $user_id);

如果您想要给定课程的所有发票,同样适用。

In your situation you have the User and the Course model and they are linked with a many to many relation which has the pair (user_id, course_id) as key. I would call this model subscription, and give to that its own identifier, and then I will link this model with the Invoice model, so your final scheme (minimal version) could be:

User:
  columns:
    id:
      type: integer
      primary: true

Course:
  columns:
    id:
      type: integer
      primary: true

Subscription:
  columns:
    id:
      type: integer
      primary: true
    user_id:
      type: integer
      notnull: true
    course_id:
      type: integer
      notnull: true
  relations:
    User:
      foreignAlias: Subscriptions
    Course:
      foreignAlias: Subscriptions

Invoice:
  columns:
    id:
      type: integer
      primary: true
    subscription_id:
      type: integer
      notnull: true
  relations:
    Subscription:
      foreignAlias: Subscription
      foreignType: One

In this way you have a normalized database and you can access invoices both from users and from courses with this code:

$user->Subscriptions->getFirst()->Invoice
$course->Subscriptions->getFirst()->Invoice

If you want all invoices for a given users you can do a query in this way

InvoiceTable::getInstance()->createQuery('i')
    ->select('i.*')
    ->leftJoin('i.Subscription s')->leftJoin('s.User u')
    ->where('u.id = ?', $user_id);

The same apply if you want all the invoices for a given course.

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