Reputation: 479
I have this problem with my view-controller relation. This is the controller:
<?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']);
}
}
It is getting the proper question and possible_answers (I can see the query output) but the view is always showing the same question (no matter what question_set_id and order_value I will pass to the action) and all the possible_answers (not only these that are actually related with the question, even this question that always shows itself). As the query output is correct, it needs to be some problem with passing data to the view, I guess. Anyway, the view looks like this:
<!-- 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');
}
?>
and the query output is like this:
1 SELECT
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
FROMmentor11
.orders
ASOrder
LEFT JOINmentor11
.questions
ASQuestion
ON (Order
.question_id
=Question
.id
) LEFT JOINmentor11
.question_sets
ASQuestionSet
ON (Order
.question_set_id
=QuestionSet
.id
) WHERE 1 = 1 LIMIT 1(affected 1, num. rows 1, took 25)
2 SELECT
Question
.id
,Question
.content
,Question
.company_id
,Question
.kind
,Company
.id
,Company
.trader_id
,Company
.name
FROMmentor11
.questions
ASQuestion
LEFT JOINmentor11
.companies
ASCompany
ON (Question
.company_id
=Company
.id
) WHERE 1 = 1 LIMIT 1(affected 1, num. rows 1, took 49)
3 SELECT
Order
.id
,Order
.question_id
,Order
.question_set_id
,Order
.value
FROMmentor11
.orders
ASOrder
WHEREOrder
.question_id
= (1)(affected 1, num. rows 1, took 28)
4 SELECT
PossibleAnswer
.id
,PossibleAnswer
.content
,PossibleAnswer
.question_id
FROMmentor11
.possible_answers
ASPossibleAnswer
WHEREPossibleAnswer
.question_id
= (1)(affected 2, num. rows 2, took 35)
5 SELECT
PossibleAnswer
.id
,PossibleAnswer
.content
,PossibleAnswer
.question_id
FROMmentor11
.possible_answers
ASPossibleAnswer
WHERE 1 = 1(affected 5, num. rows 5, took 23)
As you can see, the 4th query returns two possible answers but the view is containing five of them (for now it's all of them):
At first I suspected that the 5th query (returning all the possible_answers) is somehow "deleting" the 4th query; I still don't know how this 5th query was called (I don't need it and I can see no piece of code that would call for such a query) but I changed "my" $possible_answers to $possible_answerz just to make it different - it didn't work but even if it did: it wouldn't explain why the $question is always the same...
Any tips?
Edit: when I flash the values - they are correct...
Upvotes: 2
Views: 671
Reputation: 576
try this:
$order = $order_model -> find('first', array(
'conditions' => array(
'Order.question_set_id' => $question_set_id,
'Order.value' => $order_value)
)
);
and then add 'conditions' key to other arrays
Upvotes: 1