在 MySQL/Codeigniter 中动态引用表?
这是我的团队所坚持的用例: 系统中有不同类型的对象(例如:用户、照片、链接、视频、标签、照片标签等)。现在每个对象都有自己的表。当出于任何目的(例如实时反馈、活动跟踪、标记不同的对象类型等)查找对象时,理想情况下系统需要知道三件事:
1) 对象ID
2) 对象类型
3) 来自对象的对象详细信息,具体取决于用例
对于 1 和 3 2,我通过添加两列来处理它:object_id、object_type - 这将始终告诉我 ID 以及 ID 所指的对象。但对于第 3 步,问题是获取对象详细信息,我需要知道每个对象与哪些表相关。那么我该怎么做呢?我正在使用 MySQL 和 codeignitor php。
我能想到的一种方法是拥有一个在对象和模式表之间具有关系的表。但缺点是我必须始终加入这个以获取表名然后查找该表。我希望跳过任何加入。在 codeignitor 中是否可以为此执行任何操作或添加任何应用程序逻辑来动态检测要根据对象类型引用哪个表?也许我什至不需要 object_Type 列,系统可以仅从 object_id 和 page_id 或其他内容自行找到表?
Here is the usecase my team is stuck with:
There are different types of objects in the system, (ex: user, photo, link, video, tag, phototag, etc). Now each object has its own table. When looking up objects for any purpose (say live feed, activity tracking, tagging different object types, etc) ideally the system needs to know three things:
1) The object ID
2) The object type
3) Object details from the object's depending on the usecase
For 1 & 2, i am handling it by adding two columns: object_id, object_type - this will always tell me the ID and what object the Id is referring to. But for step 3 the problem is to get object details I need to know which tables each object relates to. So how do i do that? I am using MySQL and codeignitor php.
One way i can think of is to have a table that has a relation between object and the schema tables. But the downside is then i have to always join this to get the table name then lookup that table. I am hoping to skip any join. Is there anything that can be done within codeignitor for this or any application logic to add which can detect which table to reference based on object type dynamically? And maybe i dont even need the object_Type column and the system can find the table on its own just from the object_id and the page_id or something else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在谈论Rails 目前支持的
[多态关联][1]
。我认为 Codeigniter 默认情况下不支持此功能(使用另一个层 [ORM])。然而,我唯一能想到的就是让一个库来处理这个问题,但这都是条件语句(ifs)。You are talking about
[polymorphic associations][1]
which Rails currently supports. I don't think Codeigniter supports thisby default
(use another layer[ORM]). However, the only thing I can think of is having a library handle this but this will all be conditional statements (ifs).