【问题标题】:CakePHP model question, How can i make table relationship for polls and quiz?CakePHP 模型问题,我如何为投票和测验建立表格关系?
【发布时间】:2011-11-10 00:04:23
【问题描述】:

假设我想用一个问题和几个选项作为答案来创建民意调查和测验。

我必须为此创建投票模型吗?

如何跟踪投票表中每个选项的百分比结果?

我应该制作另一个带有投票答案选项的表格吗?

投票问题和投票答案的表关系是什么?

这是正确的方法吗?

create table polls (id integer not null auto_increment primary key, question varchar(300) not null, mark tinyint not null, created datetime, modified datetime);

create table pollsanswers (id integer not null integer auto_increment primary key, poll_id integer not null, answer varchar(500) not null, mark tityint not null, created datetime, modified datetime);


create table quizes (id integer not null auto_increment primary key, question varchar(300) not null, mark tinyint not null, created datetime, modified datetime);

create table quizesanswers (id integer not null integer auto_increment primary key, quiz_id integer not null, answer varchar(500) not null, mark tityint not null, created datetime, modified datetime);

如果我创建了一个 mysql 表 polls,那么我可以通过帖子或其他控制器访问和使用该表还是必须创建 polls_controller.php 和 poll.php 模型?

我可以在不制作新模型和控制器的情况下做到这一点吗?

【问题讨论】:

    标签: mysql sql postgresql cakephp cakephp-1.3


    【解决方案1】:

    如果是我,我可能会有以下表格:

    create table polls (
      id integer not null auto_increment primary key, 
      created datetime, 
      modified datetime
    );
    
    create table quizzes (
      id integer not null auto_increment primary key,
      created datetime,
      modified datetime
    );
    
    create table questions (
      id integer not null auto_increment primary key,
      model varchar(255) not null, -- Poll or Quiz, in this scenario
      foreign_key integer not null,
      question varchar(255) not null,
      created datetime,
      modified datetime
    );
    
    create table answers (
      id integer not null auto_increment primary key,
      question_id integer not null,
      answer varchar(255) not null,
      created datetime,
      modified datetime
    );
    

    我的联想可能是这样的:

    Poll hasMany Question
    Quiz hasMany Question
    Question belongsTo Poll, Quiz
    Question hasOne Answer
    Answer belongsTo Question
    

    由于民意测验和测验都有组成问题,我会尝试巩固这方面。为了说明这两种关系,我将Questionpolymorphic

    【讨论】:

    • 不,投票和测验的问题是不同的。我不能有一个回答专栏吗?为什么我应该为答案创建一个不同的表?您可以为所有人发布完整的创建表吗?这样会更容易理解。谢谢
    • 投票和测验的栏目可以是什么?
    • 问题本身可能不同,但它们仍然是问题,对吧?这只是将问题隔离在他们自己的表格中,以防有与问题本身相关的其他元数据。如果您愿意,当然可以将答案作为一列包含在 questions 表中。
    • Question hasOne Answer 仅适用于 Quez,不适用于 Poll。民意调查有很多百分比答案。
    • 所以一个投票有很多问题,每个问题都有很多答案?
    猜你喜欢
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-01
    相关资源
    最近更新 更多