问题
We are building a survey engine for our internal use. I would like to know how to persist the question branching logic into the database? Any body done this before or any ideas on the schema for the database?
If the user responses with an answer, we need to skip to the next questions based on the logic added to the questions Each question can have multiple logic added to it.
For eg:
Question: Is it Sunny, Raining or Cloudy?
Answer: Raining.
The next question should be based on the previous answer.
if(Raining)
{
}
if(Sunny)
{
}
if(Cloudy)
{
}
how do I persist the above to the database and go from there?
Any bright ideas ?
回答1:
You're essentially looking to persist a decision tree into a database. You'd want to store each question as a node and, in the interests of a normalized database, store the edges in a separate table relating questions that depend on other questions (directed edges), and walk as appropriate.
Edit: A simple design can be two tables: Questions and Edges. Questions just has id
and question text
. Edges can be answered_question_id
, next_question_id
, and answer
. The first table is self-explanatory. The second table lists that if a question answered_question_id
is asked and answered with something that either equals to or matches answer
, forward to question next_question_id
next.
来源:https://stackoverflow.com/questions/5278187/how-to-persist-branching-logic-into-database