我的视图控制器关系有这个问题。这是控制器:
<?php
class AnswersController extends AppController {
public $helpers = array('Html', 'Form', 'Session');
public $components = array('Session');
public function add() {
$customer_id = $this->params['url']['customer_id'];
$question_set_id = $this->params['url']['question_set_id'];
$order_value = $this->params['url']['order_value'];
$possible_answer_model = ClassRegistry::init('PossibleAnswer');
$question_model = ClassRegistry::init('Question');
$order_model = ClassRegistry::init('Order');
$order = $order_model -> find('first', array(
'Order.question_set_id' => $question_set_id,
'Order.value' => $order_value));
$question = $question_model -> find('first', array(
'Question.id' => $order['Order']['question_id']));
$this -> set('question', $question);
if ($question['Question']['kind'] != "o") {
$this -> set('possible_answers', $possible_answer_model -> find('all', array(
'PossibleAnswer.question_id' => $question['Question']['id'])));
}
$this->Session->setFlash($question['Question']['content']);
}
}
它得到了正确的问题和可能的答案(我可以看到查询输出),但视图总是显示相同的问题(无论我将传递给操作的 question_set_id 和 order_value 是什么)和所有可能的答案(不仅仅是这些实际上是与问题相关,即使这个问题总是会出现)。由于查询输出是正确的,我猜在将数据传递给视图时需要有一些问题。无论如何,视图如下所示:
<!-- File: /app/View/Answers/add.ctp -->
<?php
if ($question['Question']['kind'] == 'o') {
echo $this->Form->create('PossibleAnswer');
echo $this->Form->input('content', array(
'rows' => '3', 'label' => 'Miejsce na twoją odpowiedź:'));
echo $this->Form->input('PossibleAnswer', array(
'question_id' => $question['Question']['id']));
echo $this->Form->end('Dalej');
}
else {
echo $this->Form->create('Answer');
foreach ($possible_answers as $possible_answer) {
echo '<input name="'
.'possible_answers'
.'" id="'
.$possible_answer['PossibleAnswer']['id']
.'" value="'
.$possible_answer['PossibleAnswer']['id']
.'" type="radio">';
echo '<label for="'
.$possible_answer['PossibleAnswer']['id']
.'">'
.$possible_answer['PossibleAnswer']['content']
.'</label><br />';
}
echo $this->Form->end('Dalej');
}
?>
查询输出是这样的:
1 选择
Order
。id
,Order
.question_id
,Order
.question_set_id
,Order
.value
,Question
.id
,Question
.content
,Question
.company_id
,Question
.kind
,QuestionSet
.id
,QuestionSet
.name
,QuestionSet
.company_id
从mentor11
。orders
如左Order
加入mentor11
。questions
ASQuestion
ON (Order
.question_id
=Question
.id
) 左连接mentor11
。question_sets
ASQuestionSet
ON (Order
.question_set_id
=QuestionSet
.id
) WHERE 1 = 1 LIMIT 1(受影响 1,第 1 行,占用 25)
2 选择
Question
。id
,Question
.content
,Question
.company_id
,Question
.kind
,Company
.id
,Company
.trader_id
,Company
.name
从mentor11
。questions
如左Question
加入mentor11
。companies
ASCompany
ON (Question
.company_id
=Company
.id
) WHERE 1 = 1 LIMIT 1(受影响 1,第 1 行,占用 49)
3 选择
Order
。id
,Order
.question_id
,Order
.question_set_id
,Order
.value
从mentor11
。在orders
哪里。= (1)Order
Order
question_id
(受影响 1,第 1 行,占用 28 行)
4 选择
PossibleAnswer
。id
,PossibleAnswer
.content
,PossibleAnswer
.question_id
从mentor11
。在possible_answers
哪里。= (1)PossibleAnswer
PossibleAnswer
question_id
(受影响 2,第 2 行,占用 35)
5 选择
PossibleAnswer
。id
,PossibleAnswer
.content
,PossibleAnswer
.question_id
从mentor11
。possible_answers
1PossibleAnswer
= 1(影响 5 行,第 5 行,占用 23 行)
如您所见,第 4 个查询返回两个可能的答案,但视图包含其中五个(现在是全部):
起初我怀疑第 5 个查询(返回所有可能的答案)以某种方式“删除”了第 4 个查询;我仍然不知道第 5 个查询是如何被调用的(我不需要它,而且我看不到任何需要这样查询的代码)但我将“我的”$possible_answers 更改为 $possible_answerz 只是为了使它不同 - 它没有工作,但即使它做了:它不会解释为什么 $question 总是相同的......
有小费吗?
编辑:当我闪烁这些值时 - 它们是正确的......