2012-07-11 90 views
2

我有我的視圖控制器關係的這個問題。這是控制器: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選擇OrderidOrderquestion_idOrderquestion_set_idOrdervalueQuestionidQuestioncontentQuestioncompany_idQuestionkindQuestionSetidQuestionSetnameQuestionSetcompany_idmentor11orders AS Order LEFT JOIN mentor11。 (Orderquestion_id = Questionid)LEFT JOIN mentor11question_sets AS QuestionSet ON(Orderquestion_set_id = QuestionSetid)其中1 = 1 LIMIT 1

(受影響1,NUM。行1,耗時25)

2 SELECT QuestionidQuestioncontentQuestioncompany_idQuestionkindCompanyidCompanytrader_idCompanynamementor11questions AS Question LEFT JOIN mentor11companies AS Company ON(Questioncompany_id = Companyid)其中1 = 1 LIMIT 1

(受影響1,NUM。行1,把49)

3 SELECT OrderidOrderquestion_idOrderquestion_set_idOrdervaluementor11orders AS Order WHERE Orderquestion_id =(1)

(受影響1,NUM。行1,把28)

4 SELECT PossibleAnsweridPossibleAnswercontentPossibleAnswerquestion_idmentor11possible_answers AS PossibleAnswer WHERE PossibleAnswerquestion_id =(1)

(受影響2,貨號行2,花了35)

5 SELECT PossibleAnsweridPossibleAnswercontentPossibleAnswerquestion_idmentor11possible_answers AS PossibleAnswer WHERE 1 = 1

(影響5,NUM,行5,耗時23)

正如你可以看到,第四查詢返回兩個可能的答案,但在視圖包含其中五(現在它是所有的人): view

起初我懷疑第五個查詢(返回所有possible_answers)是以某種方式「刪除」第四個查詢;我仍然不知道第5個查詢是如何被調用的(我不需要它,並且我看不到任何代碼會調用這樣的查詢),但是我將「我的」$ possible_answers改爲$ possible_answerz,只是爲了讓它不同 - 它沒有工作,但即使它做到了:它不能解釋爲什麼$問題總是相同的...

任何提示?

編輯:當我閃的價值觀 - 它們是正確的......

回答

1

試試這個:

$order = $order_model -> find('first', array(
    'conditions' => array(
     'Order.question_set_id' => $question_set_id, 
     'Order.value' => $order_value) 
    ) 
); 

,然後添加 '條件' 鍵,其他陣列

+0

噢,我的.. 。現在它正在工作,因爲它應該。謝謝你,我的救世主! :) – smsware 2012-07-11 20:16:38

相關問題