dongsong73032 2015-07-20 02:07
浏览 46
已采纳

多个文件上传Yii 2

I'm using mysql DB, I've got table "posts" with columns id, title, text, author_id, image at the moment.

I need to provide a possibility to upload several images to one post in my blog. What's the best way of organizing my DB structure in this case and how it's usually done in Yii 2?

At the moment I just have functionality for saving 1 image and keeping it's path in table field.

Should I keep an array in DB or create another relations table?

  • 写回答

1条回答 默认 最新

  • doumu9799 2015-07-20 13:34
    关注

    When you're working with a conventional RDBMS like MySQL:
    It seems you're going from a one-to-one, to a zero- or one-to-many relation, in which case i'd recommend creating another table for your files (for example: image*), containing a foreign key image.post_id to posts.id. Added benefit is that you will be able to more neatly store some metadata about the image, instead of creating a load of extra (but perhaps unneeded columns) in the posts table.

    The cleanest solution (imho) is usually to stay close to the data structure of your DMBS, instead of placing arbitrary data structures inside a text field, no matter what framework or language you use.

    This is different when working with no-sql database like for example MongoDB, where depending on the use-case you may want to use an array property images on your posts document containing image objects.

    *yii naming convention for tables is singular instead of plural

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件