自己的ORM:JOIN情况下的数据库记录?
我们正在做自己的具有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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
DBIx::Class 通过为每个表提供一个类来处理此问题,并且连接由获取与另一个表匹配的对象的方法来表示。
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..
我认为每个类都应该代表一条记录,整个表应该是一个对象数组(或其他一些集合)。查看 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.我根据 这个 PHP UI 框架 的模型/ORM 实现提供一些见解。以下是我的一些建议:
希望这能帮助您找到自己的方法或决定不实现自己的 ORM。这不是一件容易的事。
I'm providing some insight based on the Model/ORM implementation of this PHP UI Framework . Here are some suggestions from me:
Hopefully this would help find your own way or to decide on not implementing your own ORM. It's not an easy task.