doushang1778 2011-11-25 11:56
浏览 6
已采纳

数据库结构允许用户对某个主题进行投票

I have a Job model and a User model. I need each User to be able to answer several questions based on a particular Job. Each question is marked from 1 to 10 (or unanswered).

I believe i need a hasMany through associations, but have been confused by these before.

Can someone help me with my database/model layout. Thanks.

EDIT:

  • The only tables I have at the moment are users and jobs
  • The questions are the same for every job, there are six of them (maybe more in the future)
  • The Questions will always be numbers from 1 to 10 (or unanswered)
  • 写回答

4条回答 默认 最新

  • donglan9651 2011-11-25 15:24
    关注

    Yes, perfect candidat for hasMany through, and I'll do it like so:

    User hasMay JobQuestion
    Job hasMany JobQuestion
    
    JobQuestion belongsTo Job, User
    

    put all your questions in the JobQuestion model. There might be a more appropriate name for this model. Do you need more details for the models keys?

    Edit:

    A row in the JobQuestion model would:

    id | user_id | job_id | question1 | ... | questionN

    Your data array would something like

    Array
    (
        [0] => Array
            (
                [JobQuestion] => Array
                    (
                        [id] => 1
                        [user_id] => 1
                        [job_id] => 1
                        [question1] => answer1
                        [question2] => answer2
                    )
    
                [Job] => Array
                    (
                        [id] => 1
                        [name] => somejob
                    )
    
                [User] => Array
                    (
                        [id] => 1
                        [username] => someuser
                    )
    
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 hexo+github部署博客
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?