我的视图 - 控制器关系存在这个问题。这是控制器:
<?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']);
}
}
它得到了正确的问题和possible_answers(我可以看到查询输出),但视图总是显示相同的问题(无论我将传递给action的什么question_set_id和order_value)和所有的possible_answers(不仅仅是这些这实际上与问题有关,即使这个问题总能表现出来。由于查询输出正确,我想将数据传递给视图需要遇到一些问题。无论如何,视图看起来像这样:
<!-- 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 SELECT
Order
。id
,Order
。question_id
,Order
。question_set_id
,Order
。{{1 },value
。Question
,id
。Question
,content
。Question
,company_id
。Question
,kind
。QuestionSet
,id
。QuestionSet
,name
。QuestionSet
FROMcompany_id
。mentor11
AS { {1}} LEFT JOINorders
。Order
ASmentor11
ON(questions
。Question
=Order
。question_id
)LEFT加入Question
。id
ASmentor11
ON(question_sets
。QuestionSet
=Order
。question_set_id
)WHERE 1 = 1 LIMIT 1(受影响的1,数量为1行,为25)
2 SELECT
QuestionSet
。id
,Question
。id
,Question
。content
,Question
。{{1 },company_id
。Question
,kind
。Company
,id
。Company
FROMtrader_id
。Company
ASname
LEFT JOINmentor11
。questions
ASQuestion
ON(mentor11
。companies
=Company
。Question
)WHERE 1 = 1 LIMIT 1(受影响的1,数量为1行,为49)
3 SELECT
company_id
。Company
,id
。Order
,id
。Order
,question_id
。{{1 } {FROM}Order
。question_set_id
ASOrder
WHEREvalue
。mentor11
=(1)(受影响的1,数字行1,花了28)
4 SELECT
orders
。Order
,Order
。question_id
,PossibleAnswer
。id
FROMPossibleAnswer
。{{1 AScontent
WHEREPossibleAnswer
。question_id
=(1)(受影响的2,数量为2行,为35)
5 SELECT
mentor11
。possible_answers
,PossibleAnswer
。PossibleAnswer
,question_id
。PossibleAnswer
FROMid
。{{1 ASPossibleAnswer
WHERE 1 = 1(受影响5,数量5行,需要23次)
正如您所看到的,第4个查询返回两个可能的答案,但视图中包含其中的五个(现在它们全部都是):
起初我怀疑第5个查询(返回所有的possible_answers)以某种方式“删除”第4个查询;我仍然不知道第五个查询是如何被调用的(我不需要它,我可以看到没有一段代码可以调用这样的查询)但我将“my”$ possible_answers更改为$ possible_answerz只是为了使它成为可能不同 - 它没有用,但即使它确实如此:它也无法解释为什么$问题总是一样的......
任何提示?
编辑:当我刷新值时 - 它们是正确的......
答案 0 :(得分:1)
试试这个:
$order = $order_model -> find('first', array(
'conditions' => array(
'Order.question_set_id' => $question_set_id,
'Order.value' => $order_value)
)
);
然后将'conditions'键添加到其他数组