一个控制器两种型号
假设我有一个 YouTube 风格的网站,但包含图像而不是视频。每个用户一次可以上传多张图像。我想将图像存储在一个表中,并且我想将有关显示图像的页面(图像的“辩论”页面)的信息存储在另一个表中 - 这个表有一列,我在其中存储图像的外部 ID已上传并在数据库中引用的图像。
我应该创建两个模型并从同一个控制器调用它们吗?
例如,我可以让 createDebateController
创建两个模型 - 一个用于存储辩论页面信息,另一个用于存储图像。这合理吗?如果是,我将如何将每个图像的 id
存储在在数据库中创建辩论页面信息的模型中?
Let’s say I have a YouTube-style site, but with images rather than videos. Each user can upload multiple images at a time. I want to store the images in one table, and I want to store the information about the page that displays the images (the images’ “debate” page) in another table — this one has a column where I store the foreign ID of the images that were uploaded and referenced in the database.
Should I create two models and call them both from the same controller?
For example, I could have createDebateController
create two models — one for storing the debate page info and one for storing the images. Would this be reasonable? If it is, how would I store the id
of each image in the model that creates the debate page info in the DB?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将为每个创建一个模型,然后仅通过其相关的图像对象引用辩论对象。因此,当发布新图像时:
然后,每当发布评论时,您将使用图像对象间接创建辩论对象:
或者在渲染显示页面时:
您的图像对象将直接访问辩论对象:
I would create a model for each, then only reference the debate object through its related image object. So, when posting a new image:
Then, whenever a comment gets posted, you'd use the image object to indirectly create the debate object:
Or when rendering the display page:
Your image object would then access the debate object directly:
我认为无论如何你可能会使用两个单独的控制器。一种用于辩论,一种用于图像。您的辩论模型(假设一对多关系)应该具有图像 ID 的集合,或者如果您需要每个图像的位置或其他标识信息,则可能具有图像描述符的集合。如果您不需要放置信息,则只需使用图像 ID 来使用图像控制器生成每个图像的链接集合。如果您确实需要放置(或描述等信息),则描述模型具有实际的图像 ID。是的,在暴露于视图的模型之间建立关系是可以的。
I think you probably use two separate controllers regardless. One for debates and one for images. Your debate model (assuming a one-to-many relationship) should have a collection of image ids, or maybe a collection of image descriptors if you need placement or other identifying information for each image. If you don't need placement information, you simply use the image ids to generate the collection of links to each image using the image controller. If you do need placement (or description, etc. information) then, the description model has the actual image id. Yes it is ok to have relationships between your models that are exposed to the view.