views:

32

answers:

1

I have the following model associations:

Response->Survey
Response->Question
Response->Choice
Survey->Question
Question->Choice

I want to create a form where I could answer all the questions for one survey. So I used the following to return the needed data:

$questions = $this->Response->Question->find('all', array(
    'conditions' => array('survey_id' => $id), 
    'contain' => array('Choice')
    )
);

Sample output for debug($questions).

Questions

  1. Is there a contain() option so that an associated model returns in the find('list') format so that I could use:

    foreach($question as $questions) { $this->Form->select('field_name', $question['Choice']); }

  2. If no option is available, how could I do this using PHP's builting array methods?

PS: The foreach block won't turn into a code block. If someone could edit and fix it, please do so and delete this line. Thank you.

A: 

Did it using:

$keys = array();
$values = array(); 

foreach($question['Choice'] as $choice) {
    array_push($keys, $choice['id']);
    array_push($values, $choice['choice']);
}
$choices = array_combine($keys, $values);

The 'original' choices are still inside the $questions array, unmodified. If anyone knows a better approach, please post it. Thank you.

Ramon Marco Navarro