我有我的视图控制器关系的这个问题。这是控制器:CakePHP并将变量从控制器发送到视图
<?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(我可以看到查询输出),但观点是处处显示着同样的问题(无论什么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选择
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
ASOrder
LEFT JOINmentor11
。 (Order
。question_id
=Question
。id
)LEFT JOINmentor11
。question_sets
ASQuestionSet
ON(Order
。question_set_id
=QuestionSet
。id
)其中1 = 1 LIMIT 1(受影响1,NUM。行1,耗时25)
2 SELECT
Question
。id
,Question
。content
,Question
。company_id
,Question
。kind
,Company
。id
,Company
。trader_id
,Company
。name
从mentor11
。questions
ASQuestion
LEFT JOINmentor11
。companies
ASCompany
ON(Question
。company_id
=Company
。id
)其中1 = 1 LIMIT 1(受影响1,NUM。行1,把49)
3 SELECT
Order
。id
,Order
。question_id
,Order
。question_set_id
,Order
。value
从mentor11
。orders
ASOrder
WHEREOrder
。question_id
=(1)(受影响1,NUM。行1,把28)
4 SELECT
PossibleAnswer
。id
,PossibleAnswer
。content
,PossibleAnswer
。question_id
从mentor11
。possible_answers
ASPossibleAnswer
WHEREPossibleAnswer
。question_id
=(1)(受影响2,货号行2,花了35)
5 SELECT
PossibleAnswer
。id
,PossibleAnswer
。content
,PossibleAnswer
。question_id
从mentor11
。possible_answers
ASPossibleAnswer
WHERE 1 = 1(影响5,NUM,行5,耗时23)
正如你可以看到,第四查询返回两个可能的答案,但在视图包含其中五(现在它是所有的人):
起初我怀疑第五个查询(返回所有possible_answers)是以某种方式“删除”第四个查询;我仍然不知道第5个查询是如何被调用的(我不需要它,并且我看不到任何代码会调用这样的查询),但是我将“我的”$ possible_answers改为$ possible_answerz,只是为了让它不同 - 它没有工作,但即使它做到了:它不能解释为什么$问题总是相同的...
任何提示?
编辑:当我闪的价值观 - 它们是正确的......
噢,我的.. 。现在它正在工作,因为它应该。谢谢你,我的救世主! :) – smsware 2012-07-11 20:16:38