自己的ORM:JOIN情况下的数据库记录?

发布于 2024-12-07 02:38:34 字数 211 浏览 0 评论 0原文

我们正在做自己的具有ORM能力的框架。数据库表现在是类,但是记录呢?让我们想象两个表:

Users
ID,USERNAME

Emails
USER_ID,ADDRESS

因此,一个记录对象将具有 getID()、getUSERNAME() 方法等,但如果这两个表是 JOIN 的,它就不能有两种类型,对吗?因为没有多重继承。那么场碰撞呢?

We are doing our own framework with ORM capability. The database tables are classes now, but how about records? Lets imagine two tables:

Users
ID,USERNAME

Emails
USER_ID,ADDRESS

so, a record object will have getID(), getUSERNAME() methods, etc but if the two tables are JOIN-ed, it cant have two types right? Since there is no multiple inheritance. And what about field collision?

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

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

发布评论

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

评论(3

若无相欠,怎会相见 2024-12-14 02:38:34

DBIx::Class 通过为每个表提供一个类来处理此问题,并且连接由获取与另一个表匹配的对象的方法来表示。

$myAddress = $myUser->emails->address;

DBIx::Class handles this by having a Class for each table, and joins are represented by a method that gets an object matching the other table..

$myAddress = $myUser->emails->address;
乜一 2024-12-14 02:38:34

我认为每个类都应该代表一条记录,整个表应该是一个对象数组(或其他一些集合)。查看 http://www.doctrine-project.org/ 以获得一些想法。

对于 JOIN,您应该有一些定义别名的机制。这样,您就可以处理现场碰撞。

对于 getter 和 setter,您可以使用 __call__get__set。请参阅http://php.net/manual/en/language.oop5.overloading。 php 了解更多信息。

I think every class should represent a record and a whole table should be an array (or some other collection) of objects. Take a look at http://www.doctrine-project.org/ to get some ideas.

And for JOIN, you should have some mechanism for defining aliases. That way, you can deal with field collision.

And for getters and setters, you can use __call, __get and __set. See http://php.net/manual/en/language.oop5.overloading.php for more info.

无法言说的痛 2024-12-14 02:38:34

我根据 这个 PHP UI 框架 的模型/ORM 实现提供一些见解。以下是我的一些建议:

  • 不要盲目决定将函数映射到字段中。为什么不使用 get('field') 和 set('field')。没有任何缺点(除了缺乏 IDE 提示之外),但您可以避免代码生成或包罗万象,这通常会比较慢。
  • 加入时您不一定需要多个对象。在我的 ORM 中,单个模型可以与连接表一起使用。这引入了透明度,当您调用 $model->set('address') 时,它可能与连接表相关联。我仍在使用动态查询的子实例进行子选择,但对于连接则不需要。
  • 我看到了继承的强大功能以及在父模型中重塑父模型的能力。每个表可以有根据您的业务用途有多个模型。
  • 模型和 ORM 应该分开,但应该非常紧密地结合在一起。我还设法让通用视图和通用控制器使一切都能很好地发挥作用,这可以节省大量时间。

希望这能帮助您找到自己的方法或决定不实现自己的 ORM。这不是一件容易的事。

I'm providing some insight based on the Model/ORM implementation of this PHP UI Framework . Here are some suggestions from me:

  • Don't decide blindly to map functions into fields. Why not use get('field') and set('field'). There is no downside (apart from lack of IDEs hinting), but you can avoid code generation or catch-all which usually is slower.
  • When joining you wouldn't necessarily want multiple objects. In my ORM a single Model can work with joined tables. This introduces transparency and when you call $model->set('address') it might be associated with joined table. Im still using sub-instance of a dynamic query for sub-selects but for joins there is no need.
  • I've see a lot of power of inheritance and ability to re-shape parent models in parent model. Each table can have multiple models depending on your business uses.
  • Models and ORM should be separated but should play together very closely. I've also managed to make everything play well with generic views and generic controllers, which is a great time-saver.

Hopefully this would help find your own way or to decide on not implementing your own ORM. It's not an easy task.

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