mysql - CakePHP 模型问题,如何为民意调查和测验建立表关系?

标签 mysql sql postgresql cakephp cakephp-1.3

假设我想创建带有问题和多个选项作为答案的民意调查和测验。

我必须为此创建民意调查模型吗?

我如何跟踪民意调查表中每个选项的百分比结果?

我应该制作另一个带有民意调查答案选项的表格吗?

民意调查问题和民意调查答案的表关系是什么?

这是正确的方法吗?

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 ,那么我可以通过 posts 或其他 Controller 访问和使用该表,还是必须创建 polls_controller.php 和 poll.php 模型?

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

最佳答案

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

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

由于民意调查和测验都有组成部分的问题,我会尝试巩固这方面。为了解释这两种关系,我会提出问题 polymorphic .

关于mysql - CakePHP 模型问题,如何为民意调查和测验建立表关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7297854/

相关文章:

mysql - 在 MySQL 中连接表并聚合数据?

python - 比较两个数据库并找到连续的共同值

SQL 左外连接不提供完整表

linux - 将 .text 文件插入 postgresql 文本字段

postgresql - Postgres 接受任何密码

postgresql - 您可以在 sequelize 中定义带有句点的列以存储在 postgres 数据库中吗?

java - 您推荐使用什么 ec2 镜像 (ami) 来使用 java 5、jboss、mysql、apache?

mysql - 从单个数据源并行运行代码

java - Openshift MySQL 连接器

MySQL 选择帮助;子查询?