Kohana (KO3) ORM 通用数据透视表(字段:model1、model1_id、model2、model2_id)—可能的?
我有以下模型设置:
User
- has_many File (for userpics)
Gallery
- has_many File (for images)
Page
- has_one File (for background image)
页面对象可以与一个或多个图库对象共享一个文件对象作为其背景。 类似或类似的新模型
Product
- has_many File
在稍后的某个阶段,应用程序中可能会出现
。请注意,我需要一个文件模型,而不仅仅是存储实际文件的路径,因为文件模型实际上可以引用文件系统中的多个文件
File
- id
- path
- path_poster
- path_m4v (HTML5 videos need up to three files for compatibility)
- path_webm
- path_ogv
- width
- height
- poster_width
- poster_height
- type
- ... etc....
那么,是否有一种简单的方法(不覆盖整个 ORM 类)来实现以下关系:将使用具有以下字段的“通用”数据透视表:
model_name VARCHAR(8) (or model_type_id TINYINT for speed)
model_id INT
file_id INT
relation_name VARCHAR(8) (e.g., Page model can have "background" and "logo" relation)
position INT
原因:我希望有一个通用应用程序模块来检查“孤立”文件,并且还能够告诉每个文件附加的内容,因此,例如,从图库中删除文件时,应用程序会发出警告文件仍然附加到页面作为背景。
I have the following setup of models:
User
- has_many File (for userpics)
Gallery
- has_many File (for images)
Page
- has_one File (for background image)
Page object could share a File object with one or more Gallery objects for its background. And at some stage later a new model like
Product
- has_many File
or similar can appear in the App.
Note that i need a File model instead of just storing paths to the actual files because the File model can reference, in fact, several files in the filesystem
File
- id
- path
- path_poster
- path_m4v (HTML5 videos need up to three files for compatibility)
- path_webm
- path_ogv
- width
- height
- poster_width
- poster_height
- type
- ... etc....
So, is there a simple way (without overwriting the whole ORM class) to implement relationships that would use a "generic" pivot table with the following fields:
model_name VARCHAR(8) (or model_type_id TINYINT for speed)
model_id INT
file_id INT
relation_name VARCHAR(8) (e.g., Page model can have "background" and "logo" relation)
position INT
The reason: i want to have a universal App module for checking for "orphaned" files, and also be able to tell to what each file is attached, so, for example, when deleting a file from the Gallery the App would warn that the file is still attached to a Page as background.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短的回答是否定的。原因是 model_id 会与其他模型 ID 冲突。如果需要,您可以在 model_name+model_id 上创建唯一索引,但要连接这两列将需要重写 ORM 方法以通过模型的关系将模型连接在一起。
老实说,我会坚持使用简单的数据透视表。
Short answer is no. The reason is because model_id would conflict with other model IDs. If you wanted, you could do a unique index on model_name+model_id, but to join those two columns would require that you rewrite the ORM methods to join models together via their relationships.
I would honestly stick with simple pivot tables.