我有我的視圖控制器關係的這個問題。這是控制器: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